简体   繁体   English

如何在symfony2中创建动态表单

[英]how to create dynamic form in symfony2

I have a formtype, ContactoType, this form contain the next fields: 我有一个表单类型ContactoType,此表单包含以下字段:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nombre','text', array(
                'attr' => array(
                    'placeholder' => 'contacto.nombre'
                )
            ))
            ->add('departamento', 'entity', array(
                'label' => "Departamentos",
                'class' => 'ProductosBundle:Departamento',
                'property' => 'nombre'
            ))
            ->add('fechaEvento', 'birthday',array(
                'input' => 'datetime',
                'widget' => 'single_text',
                'format' => 'dd-MM-yyyy',
                'attr' => array(
                    'placeholder' => 'DD-MM-YYYY',
                    'class' => 'form-control')))
            ->add('promocion','text', array(
                'attr' => array(
                    'placeholder' => 'contacto.promocion'
                )
            ))
            ->add('apodo','text', array(
                'attr' => array(
                    'placeholder' => 'contacto.apodo'
                )
            ))
            ->add('file','file', array(
                'attr' => array(
                    'placeholder' => 'contacto.fichero'
                )
            ))
            ;
    }

The Departamento entity has a field named "requiresadditional" if this is true, and promotion nickname will be displayed, if false they are hidden. 如果为true,则Departamento实体具有一个名为“ requiresadditional”的字段,如果为false,则将显示促销昵称(如果为false),则将其隐藏。 Do not know how you could get the field value "requiresadditional"... 不知道如何获取字段值“ requiresadditional” ...

As it should do this?Thank! 应该这样做吗?谢谢!

You should test your Departamento entity in a FormEvent. 您应该在FormEvent中测试您的Departamento实体。 Read the documentation about FormEvents here . 此处阅读有关FormEvents的文档。

通常,我将所有字段添加到我的symfony表单中,并使用简单的javascript事件显示/隐藏该字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM