子表格选项

时间:2018-09-07 12:36:17

标签: forms symfony-3.4

我如何将表单选项提供给子表单?

在下面的示例中,我声明了“特殊”选项。 我想在我的子表单中访问“特殊”选项。

我的主要形式:

class DemandeType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title', TextType::class, []);

        $builder->add('service_agent', ServiceType::class, [
            'mapped' => false
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'special' => true
        ]);
    }

还有我的子窗体:

class ServiceType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        dump($options['special']);
        $builder->add('service', TextType::class, []);

        $builder->add('agent', TextType::class, [
            'mapped' => false
        ]);
    }
    public function configureOptions(OptionsResolver $resolver)
    {
    }

1 个答案:

答案 0 :(得分:0)

我回答了我的问题。

我必须在子窗体中声明选项“ special”。 而且该字段的选项可以在主表单中访问。

喜欢:

class DemandeType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title', TextType::class, []);

        $builder->add('service_agent', ServiceType::class, [
            'mapped' => false,
            'special' => $options['special']
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'special' => true
        ]);
    }

在我的子表单中:

class ServiceType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        dump($options['special']); // It's OK :)
        $builder->add('service', TextType::class, []);

        $builder->add('agent', TextType::class, [
            'mapped' => false
        ]);
    }
    public function configureOptions(OptionsResolver $resolver)
    {
       $resolver->setDefaults([
            'special' => true
        ]);
    }