du4373 2012-04-19 17:33
浏览 199
已采纳

使用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?

  • 写回答

1条回答 默认 最新

  • 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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab