Symfony 3:将 $options 参数值从 formType 发送到 underFormType [英] Symfony 3 : Sending the $options argument values from formType to underFormType

查看:35
本文介绍了Symfony 3:将 $options 参数值从 formType 发送到 underFormType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 formType (TagsType) 在包中创建了一个标签系统,我将其包含在我的主要 formType 中(见下文).我想知道如何将 $options 参数值从 MyFormType 发送到 TagsType.

I've created a tag system in a bundle using a formType (TagsType) which I include in my main formType (See below). I'd like to know how I can send the $options argument values from MyFormType to TagsType.

//...

use EC\TagBundle\Form\Type\TagsType;

class MyFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {


    $builder        

    //...
        ->add('tags', TagsType::class)
    //...
    ;
    }
}

我的标签类型

//...

use Symfony\Component\Form\Extension\Core\Type\TextType;

class TagsType extends AbstractType
{
    /**
     * @var ObjectManager
     */
    private $manager;

    public function __construct(ObjectManager $manager)
    {
        $this->manager = $manager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->addModelTransformer( new CollectionToArrayTransformer(), true )
            ->addModelTransformer( new TagsTransformer($this->manager), true )
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefault('attr', [
            'class' => 'tag-input',

        ]);
        $resolver->setDefault('required', false);
    }

    public function getParent()
    {
        // Il retourne un TextType par défaut.
        return TextType::class;
    }
}

推荐答案

我找到了.只需这样做:

I found. Just do this :

->add('tags', TagsType::class, ['empty_data' => $options])

TagsType.php

TagsType.php

$options = $options['empty_data'];

这篇关于Symfony 3:将 $options 参数值从 formType 发送到 underFormType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆