2012-05-24 84 views
1

我有沒有Symfony2的形式 - 與所有複選框實體場檢查

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設置所有複選框默認爲選中? 類定義是(我刪除getter和setter方法)

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(); 
    } 
} 

回答

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), 
      ) 
    ); 
0

必須與所有disabilityType初始化表單

,你可以做到這一點像:

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

這不是很好的解決方案,因爲我使用的表單沒有類,如開頭所述。 – koral