簡體   English   中英

使用formbuilder在組合框中選擇默認值

[英]Selecting default value in a combobox using formbuilder

我在嘗試選擇ComboBox(html選擇標記)中的默認值時遇到問題,使用Symfony2 FormBuilder。 這是我的代碼:

MyController.php我發送到Form的默認省份進行選擇

$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”,它可以工作,但我無法通過“data”選擇默認值。

對象在類中很好,如果我使用 - > getIdProvice()我得到對象的PK,而錯誤是因為是一個字符串。

我看了一些信息,但我不知道該怎么辦:

如何在Symfony2中設置表單字段的默認值? http://symfony.com/doc/current/reference/forms/types/field.html

有人看到任何錯誤嗎?

如果您需要默認值,則需要在創建表單之前在實體中設置默認值。

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

但在你的情況下,我不確定安裝者,你可以添加你的實體嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM