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

当我试图呈现表单时,我只得到集合字段,集合下的子字段没有显示。

我正在使用树枝,我试图循环通过形式,但什么也没有出现,除了收集字段。

我哪里错了?

由于某种原因,我无法在父类的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)
            );
        }

最新更新