在Collection类中使用Symfony表单选项

时间:2017-07-10 07:53:16

标签: php forms symfony

//控制器部件

 $form = $this->createForm($formType, $user, [ 'id' => $user->getId() ]);

//表单类型部分

 public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $id = $options['id'];

        $builder->add('profile_person_affiliations', CollectionType::class, array(
            'label' => 'Affiliation',
            'entry_type' => \SciProfileBundle\Form\ProfilePersonAffiliationType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'prototype_name' => '__name__',
            'by_reference' => false,
        ));

    }

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'id' => null
    ));
}

//集合表单类型部分

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $id = $options['id'];

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'SciProfileBundle\Entity\ProfilePersonAffiliations',
            'id' => null
        ));
    }

我需要在集合中使用选项。现在,我可以在Parrent表单类型中使用id,但不能在Collection Form类型中使用。在子项中获取选项数据表单的最佳做法是什么 - 集合表单类型?

1 个答案:

答案 0 :(得分:1)

您可以使用entry_options

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $id = $options['id'];

    $builder->add('profile_person_affiliations', CollectionType::class, array(
        'label' => 'Affiliation',
        'entry_type' => \SciProfileBundle\Form\ProfilePersonAffiliationType::class,
        'entry_options' => array('my_custom_option' => $id),
        'allow_add' => true,
        'allow_delete' => true,
        'prototype' => true,
        'prototype_name' => '__name__',
        'by_reference' => false,
    ));

}