使用formbuilder在组合框中选择默认值

时间:2012-04-19 17:33:52

标签: php symfony formbuilder

我在尝试选择ComboBox(html选择标记)中的默认值时遇到问题,使用Symfony2 FormBuilder。这是我的代码:

MyController.php 我发送到表单的默认省份

$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 我在省字段中使用默认省份

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 实体

<?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; }

}

显然每件事都很好,但它不起作用。如果我使用“preferred_choices”,它可以工作,但我无法通过“数据”选择默认值。

对象在类中很好,如果我使用 - &gt; getIdProvice()我得到对象的PK,而错误是因为是一个字符串。

我读了一些信息,但我不知道该怎么做:

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

有人看到有任何错误吗?

1 个答案:

答案 0 :(得分:2)

如果您需要默认值,则需要在创建表单之前在实体中设置默认值。

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

但是在你的情况下,我不确定是谁,你可以添加你的实体吗?