自定义FormBuilderInterface Symfony 3

时间:2017-12-11 05:44:08

标签: forms symfony

我在symfony上获得了FormType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('statement', TextType::class, array(
            'label' => 'Enunciado:',
            "required"=>"required", "attr"=>array(
                "class" => "form-name form-control"
            )))
        ->add('period', ChoiceType::class, array(
            'label' => 'Periodo:',
            'choices'  => array(
                '2017-2018' => "1718",
                '2018-1029' => "1819"
            ),
            "required"=>"required", "attr"=>array(
                "class" => "form-name form-control"
            )))
        ->add('questiontype', ChoiceType::class, array(
            'label' => 'Tipo de pregunta:',
            'choices'  => array(
                'Test' => "test",
                'Relacion' => "relacion",
                'Desarrolo' => "desarrollo",
                'Verdadero-Falso' => "truefalse",
            ), "attr"=>array(
                "class" => "form-password form-control"
            )))
        ->add('image', FileType::class, array('label' => 'Añadir imagen'))
        ->add('Registrar',SubmitType::class, array("attr"=>array(
            "class" => "form-submit btn btn-success"
        )));
}

我想让FileType输入可见,以防他们选择“Desarrolo”。

有没有这样做? 我正在思考一些javascript,但如果我使用FormBuilder,我不知道该怎么做。

有任何帮助吗?感谢。

2 个答案:

答案 0 :(得分:0)

是的,您可以,请查看此官方文档How to Dynamically Modify Forms Using Form Events

答案 1 :(得分:0)

我认为在你的情况下你应该使用两个表单事件:

  • PreSetData:如果问题类型已设置为“Desarrolo”,则显示FileType
  • PostSubmit:为了在表单提交后添加FileType

也许是这样的:

$builder->addEventSubscriber(new AddFileFieldPreSetDataSubscriber());

$builder->get('questiontype')->addEventSubscriber(new AddFileFieldPostSubmitSubscriber());