2016-08-26 3 views
1

私はSymfony 2フォームを使っています。このプロジェクトの一環として、ラジオ・フィールド・コレクションを作成したいと考えています。以下の私のコードの構造、symfony 2ダイナミックラジオフィールドコレクションを作成する

コントローラを参照してください

$gridElements = array(
    array(
     'field_name' => 'grid_qd_1', 
     'field_label' => '1. Difficulty in Opening a tight or new jar', 
     'section_header' => "Grid 1" 
    ), 
    array(
     'field_name' => 'grid_qd_2', 
     'field_label' => '2. Do heavy household chores (e.g., wash walls, floors)', 
     'section_header' => "Grid 1" 
    ), 
    array(
     'field_name' => 'grid_qd_3', 
     'field_label' => '3. Carry a shopping bag or briefcase', 
     'section_header' => "Grid 1" 
    ), 
    array(
     'field_name' => 'grid_qd_4', 
     'field_label' => '4. Use a knife to cut food', 
     'section_header' => "Grid 1" 
    ) 
); 
$form = $this->createForm(new GridType($gridElements)); 

MyBundle /フォーム/タイプ/ GridType.php

class GridType extends AbstractType 
{ 
    public function __construct($gridData) 
    { 
     $this->gridData = $gridData; 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add("grid", CollectionType::class, array(
      "label" => "Main Grid", 
      'attr' => array(
       'has_branching' => 1, 
       "branching_logic" => 0, 
       "section_header" => $this->gridData[0]['section_header'] 
      ) 
     )); 

     foreach ($this->gridData as $key => $data) { 
      $builder->get("grid")->add('radio_'.$key, new GridItemType($data)); 
     } 

    } 
} 

MyBundle /フォーム/タイプ/ GridItemType.php

class GridItemType extends AbstractType 
{ 
    private $gridItem; 

    public function __construct($gridItem = array()) { 
     $this->gridItem = $gridItem; 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('item', ChoiceType::class, array(
      "label" => $this->gridItem['field_label'], 
      'choices' => array(
       '3' => '3', 
       '4' => '4', 
       '5' => '5', 
      ), 
      'expanded' => true, 
      'multiple' => false, 
      'attr' => array(
       'has_branching' => 1, 
       "branching_logic" => 0, 
       "section_header" => $this->gridItem['section_header'] 
      ) 

     )); 

     if (!is_null($this->gridItem)){ 
      $builder->setData($this->gridItem); 
     } 
    } 
} 

フォームをレンダリングしようとすると、コレクションフィールドのみが取得され、コレクションの下の子フィールドは取得されません表示されます。

私は小枝を使用していますが、フォームをループしてみましたが、コレクションフィールド以外は何も出ていません。

どこが間違っていたのですか?

答えて

0

何らかの理由で、私は親のformTypeビルドメソッドで子を追加できませんでしたが、これがどのように動作するのですか。

GridType

class GridType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('grid', CollectionType::class, array(
      'label' => 'Main Grid', 
      'entry_type' => GridItemType::class, 
      'attr' => array(
       'has_branching' => 1, 
       'branching_logic' => 0, 
       'section_header' => 'Main', 
      ) 
     )); 
    } 

    public function getName() 
    { 
     return 'grid_type'; 
    } 
} 

GridItemType

class GridItemType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('item', ChoiceType::class, array(
      "label" => $options['field_label'], 
      'choices' => array(
       '3' => '3', 
       '4' => '4', 
       '5' => '5', 
      ), 
      'expanded' => true, 
      'multiple' => false, 
      'attr' => array(
       'has_branching' => 1, 
       "branching_logic" => 0, 
       "section_header" => $options['section_header'] 
      ) 
     )); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setRequired([ 
      'field_name', 'field_label', 'section_header' 
     ]); 

     $resolver->setDefaults([ 
      'auto_initialize' => false 
     ]); 
    } 

    public function getName() 
    { 
     return 'grid_item_type'; 
    } 
} 

アクション

$gridElements = array(
      array(
       'field_name' => 'grid_qd_1', 
       'field_label' => '1. Difficulty in Opening a tight or new jar', 
       'section_header' => "Grid 1" 
      ), 
      array(
       'field_name' => 'grid_qd_2', 
       'field_label' => '2. Do heavy household chores (e.g., wash walls, floors)', 
       'section_header' => "Grid 1" 
      ), 
      array(
       'field_name' => 'grid_qd_3', 
       'field_label' => '3. Carry a shopping bag or briefcase', 
       'section_header' => "Grid 1" 
      ), 
      array(
       'field_name' => 'grid_qd_4', 
       'field_label' => '4. Use a knife to cut food', 
       'section_header' => "Grid 1" 
      ) 
     ); 

     $form = $this->createForm(GridType::class, null); 

     foreach ($gridElements as $key => $gridElement) { 
      $form->get('grid')->add(
       $this->get('form.factory') 
        ->createNamed('radio_' . $key, GridItemType::class, null, $gridElement) 
      ); 
     } 
+0

おかげでたくさんダン、あなたは私の一日行わ。 – user1934061

関連する問題