Symfony2在一种形式的form_types之间形成验证依赖关系

时间:2014-01-26 08:45:04

标签: php forms validation symfony

在我的symfony2项目中,我创建了一个名为“ ChoiceAndOrTextType ”的新 FormType ,这是一个选项列表(复选框),但用户也可以检查“其他” “选项并将他的答案写入 textfield

这个代码在bschussek的答案中就像这个: Use a conditional statement when creating a form

$builder
            ->add('choice', 'choice', array(
                'choices' => $options['choices'] + array('Other' => 'Other'),
                'required' => false,
            ))
            ->add('text', 'text', array(
                'required' => false,
            ))
            ->addModelTransformer(new ValueToChoiceOrTextTransformer($options['choices']))
        ;

现在我想要正确验证这一点,所以当用户检查“其他”时,需要填写文本字段,如果未选中“其他”,则可以空白。 (依赖验证的种类)。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要使用两个validation constraints,例如在validation.yml yaml文件中

相关问题