【问题标题】:Symfony2 form - entity field with all checkboxes checkedSymfony2 表单 - 选中所有复选框的实体字段
【发布时间】:2012-06-01 19:27:14
【问题描述】:

我有一个没有类的表单

class ProfilesSearchType extends AbstractType {

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
                ->add('disabilityType', 'entity', array(
                    'class' => 'AldenXyzBundle:DisabilityType',
                    'property' => 'name',
                    'multiple' => true,
                    'expanded' => true,
                ))
        ;
    }

在控制器中调用

public function listAction()
{
    $form = $this->createForm(
        new \Alden\XyzBundle\Form\Type\ProfilesSearchType(), array());
    if (isset($_GET['profile_search']))
    {
        $form->bindRequest($request);
        $d = $form->getData();
        // some stuff here
    }
    return array(
        'form' => $form->createView()
    );
}

如何将disabilityType中的所有复选框设置为默认选中? 类定义是(我删除了setter和getter)

class DisabilityType {

    /**
     * @var integer $disabilityTypeId
     *
     * @ORM\Column(name="disability_type_id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $disabilityTypeId;

    /**
     * @var string $name
     *
     * @ORM\Column(name="name", type="string", length=50, nullable=false)
     */
    private $name;

    /**
     * @var Profile
     *
     * @ORM\ManyToMany(targetEntity="Profile", mappedBy="disabilityType")
     */
    private $profile;

    public function __construct()
    {
        $this->profile = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

【问题讨论】:

    标签: forms symfony


    【解决方案1】:

    我在控制器中添加了

    $disabilityDegree = $this->getDoctrine()->
        getRepository("AldenXyzBundle:DisabilityDegree")->findAll();
    $form = $this->createForm(new \Alden\XyzBundle\Form\Type\ProfilesSearchType(), array(
            'disabilityType' => new \Doctrine\Common\Collections\ArrayCollection($disabilityType),
                )
        );
    

    【讨论】:

      【解决方案2】:

      你必须用所有的disabilityType初始化你的表单

      你可以这样做:

      $disabilities = $this->getDoctrine()->getRepository("AldenXyzBundle:DisabilityType")-  >findAll();
      $form = $this->createForm(new \Alden\XyzBundle\Form\Type\ProfilesSearchType(), $disabilities);
      

      【讨论】:

      • 这不是一个好的解决方案,因为我使用了开头提到的没有类的表单。
      猜你喜欢
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      相关资源
      最近更新 更多