2012-04-19 149 views
0

我试图在组合框(html选择标签)中使用Symfony2 FormBuilder选择默认值时出现问题。这里是我的代码:使用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”,它可以工作,但我无法通过“数据”选择默认值。

如果我使用 - > getIdProvice()获取对象的PK,并且由于是字符串,则该对象在类中很好。

我看了一些资料,但我不知道该怎么做:

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

是否有人看到任何错误?

回答

2

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

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

但在你的情况我不知道有关的制定者,你可以添加你的实体吗?

+0

我已经发布了实体。在哪里我必须添加此代码?控制器? FormBuilder? – unairoldan 2012-04-20 14:12:08

+0

不好意思迟到了,你必须在你的'$ form = $ this-> createForm(new NewsletterType($ province),$ n)之前加上这个';在你的情况下$ entity应该是$ n,你只需要设置你的省份(或者如果你有两个实体之间的关系是倍数),希望我很清楚。 – Snroki 2012-04-23 13:51:12

+0

它的工作原理。谢谢....但是,出于好奇,如果我使用的表单没有实体类? – unairoldan 2012-05-19 18:33:38