symfony 2 创建动态无线电场集合
symfony 2 create dynamic radio fields collection
我正在使用 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/Form/Type/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/Form/Type/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);
}
}
}
当我尝试呈现表单时,我只得到集合字段,集合下的子字段没有显示。
我正在使用 twig,我尝试循环遍历表单,但除了收集字段外什么也没有出现。
我哪里错了?
出于某种原因,我无法在父级的 formType 构建方法中添加子级,但这就是我让它工作的方式。
网格类型
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)
);
}
我正在使用 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/Form/Type/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/Form/Type/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);
}
}
}
当我尝试呈现表单时,我只得到集合字段,集合下的子字段没有显示。
我正在使用 twig,我尝试循环遍历表单,但除了收集字段外什么也没有出现。
我哪里错了?
出于某种原因,我无法在父级的 formType 构建方法中添加子级,但这就是我让它工作的方式。
网格类型
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)
);
}