2012-04-19 17:33 浏览 130


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') {

    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('status', 'hidden');
        $builder->add('created', 'hidden');

    public function getName()
        return 'newsletter';

SYS_TProvince.php The entity


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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答 默认 最新

  • 已采纳
    douwen3362 douwen3362 2012-04-20 11:43

    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?

    点赞 评论 复制链接分享