我有一个包含不同问题(项目)的评估,每个项目都有特定数量的可能选择。
我需要传递选项的数量和每个问题的标签来为每个项目(问题)构建表单。
我可以将变量从“父”表单传递到表单集合,但我不知道如何传递特定于评估“答案”属性中每次迭代的变量。
在我的控制器中:
$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
));
}
}
谢谢!:)