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

使用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 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决