【问题标题】:Symfony2 Form validation dependency between form_types in one formSymfony2 Form_types之间的表单验证依赖关系在一个表单中
【发布时间】:2014-01-26 08:45:04
【问题描述】:

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

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']))
        ;

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

我该怎么做?

【问题讨论】:

    标签: php forms validation symfony


    【解决方案1】:

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

    【讨论】:

    • 是的,但这次验证约束取决于以相同形式做出的选择?我怎么得到这个?
    猜你喜欢
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    相关资源
    最近更新 更多