Search code examples
phpsymfonyformbuilder

Selecting default value in a combobox using formbuilder


I have a problem trying to select the default value in a ComboBox (html select tag) usign Symfony2 FormBuilder. Here is my code:

MyController.php I send to the Form th default province to be selected

$n = new Foo();

$em = $this->getDoctrine()->getEntityManager();
$province = $em->getRepository('MyEntityBundle:SYS_TProvince')->find('ES-M');

$form = $this->createForm(new NewsletterType($province), $n);

$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
    $form->bindRequest($request);

    if ($form->isValid()) {
        // some action
    }
}

NewsletterType.php I use the default province in the province field

class NewsletterType extends AbstractType
{
    private $province;

    function __construct($province)
    {
        $this->province = $province;
    }

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('idnewsletter', 'hidden');
        $builder->add('email', 'email');
        $builder->add('type', 'entity', 
            array('label' => 'type',
                'class' => 'MeediamSplashBundle:USR_TType',
                'property' => 'description',
                'preferred_choices' => array(3,5,7)
            ));
        $builder->add('province', 'entity', 
            array('label' => 'province',
                'class' => 'MeediamSplashBundle:SYS_TProvince',
                'property' => 'name',
                'data' => $this->province
            ));
        $builder->add('postalcode');
        $builder->add('status', 'hidden');
        $builder->add('created', 'hidden');
    }

    public function getName()
    {
        return 'newsletter';
    }
}

SYS_TProvince.php The entity

<?php

namespace SciOf\Meediam\SplashBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="SYS_TProvince")
 */
class SYS_TProvince
{
    /**
     * @ORM\Id
     * @ORM\Column(type="string", length=5, nullable=false)
     */
    protected $idprovince;

    /**
     * @ORM\Column(type="string", length=3, nullable=false)
     * @Assert\NotBlank()
     */
    protected $idcountry;

    /**
     * @ORM\Column(type="string", length=60, nullable=false)
     * @Assert\NotBlank()
     */
    protected $name;

    public function getIdprovince()             { return $this->idprovince; }
    public function getIdcountry()              { return $this->idcountry; }
    public function getName()                   { return $this->name; }
    public function setIdprovince($idprovince)  { $this->idprovince = $idprovince; }
    public function setIdcountry($idcountry)    { $this->idcountry = $idcountry; }
    public function setName($name)              { $this->name = $name; }

    public function __toString()                { return $this->idprovince; }

}

Apparently every thing is Ok, but it does not work. If I use "preferred_choices", it works, but I can not select a default value via "data".

The object is in the class well, if I use ->getIdProvice() I get the PK of the object, and an error because is a string.

I read some info, but I do not know how to do:

How to set default value for form field in Symfony2? http://symfony.com/doc/current/reference/forms/types/field.html

Does someone see any error?


Solution

  • if you want a default value you need to set your default value in your entity before creating the form.

    Like $yourEntity->setProvince('my default value');

    But in your case i'm not sure about the setter, can you add your entities please?