如何将特定于迭代的变量传递给Symfony 3中表单集合中的表单生成器?

时间:2017-05-06 17:50:42

标签: php symfony formbuilder symfony-3.2

我的评价有不同的问题(项目),每个项目都有特定数量的可能选择。

我需要传递每个问题的选项和标签的数量,以便为每个项目(问题)构建表单。

我可以从"父母"传递变量形式到表单集合但我无法弄清楚如何在评估中传递特定于每次迭代的变量"答案"属性。

在我的控制器中:

$evaluation = new Evaluation();
$answers = array();
// i set each answer to default 0, for the form to place them in view
foreach ($items as $index => $item) {
    $answers[$index] = array('value' => 0, 'item_id' => $item->getId());
}
$evaluation->setAnswers($answers);

$formBuilder = $this->createFormBuilder($evaluation);

$formBuilder->add("answers", CollectionType::class, array(
    'entry_type' => AnswerType::class,
    //here i pass all the items objects array to the AnswerType form.
    'entry_options' => array(
        'items' => $items,
    ),
    'allow_delete' => false,
    'allow_add' => false,
));

而不是在AnswerType表单类中:

class AnswerType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $choices = array();

        // here I have all the items available in $options['items'];
        // but each item has a specific number of choices, and I need the current one that is being built
        // and I also need other info form the item like the question it self

        for($i = 1; $i <= $current_item_number_of_choices_that_i_dont_have;$i++){
            $choices[$i] = $i;
        }

        $builder->add('value', ChoiceType::class, array(
            'choices_as_values' => true,
            'multiple'=>false,
            'expanded'=>true,
            'choices' => $choices,
            'label' => $current_item_text_label_that_I_also_need
        ));

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'items' => null
        ));
    }
}

谢谢! :)

1 个答案:

答案 0 :(得分:2)

可能CollectionType不是您现在需要的类型,因为它隐藏了您需要处理的图层(当前项)。所以最好的方法是自己添加答案集。像这样:

class AnswerCollectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        foreach ($options['items'] as $index => $item) {
            $builder->add($index + 1, AnswerType::class, [
                'item' => $item,
            ]);
        }
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setRequired('items');
    }
}

class AnswerType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $item = $options['item'];

        $choices = [];
        for ($i = 1; $i <= $item->getNumChoices(); $i++) {
            $choices[$i] = $i;
        }

        $builder->add('value', ChoiceType::class, [
            'label' => $item->getLabel(),
            'choices' => $choices,
            'expanded' => true,
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setRequired('item');
    }
}

请注意,AnswerType现在需要item选项才能正确构建当前选择表单。

$formBuilder->add('answers', AnswerCollectionType::class, [
    'items' => $items,
])

另一方面,CollectionType的解决方法更复杂(通过使用事件)需要静态内部计数器来了解当前项目。

有关详细信息,请参阅http://symfony.com/doc/current/form/dynamic_form_modification.html