【问题标题】:Symfony FormType at least one radio selectedSymfony FormType 至少选择了一个收音机
【发布时间】:2018-06-14 13:34:57
【问题描述】:

我正在开发 Symfony 3.4,我有一个带有多个字段和 2 个布尔值的 FormType,例如:

           ->add("is_my_first_boolean", ChoiceType::class, array(
                "expanded" => true,
                "multiple" => false,
                "choices" => array(
                    'Yes' => "1",
                    'No' => "0"
                )
            ))
            ->add("is_my_second_boolean", ChoiceType::class, array(
                "expanded" => true,
                "multiple" => false,
                "choices" => array(
                    'Yes' => "1",
                    'No' => "0"
                )
            ))

所以用户可以在我的表单上选择 2 个布尔值是/否,而我需要的是一个验证(后端的 PHP 验证,而不是前端),就像选择了这两个布尔值中的至少一个一样。

因此,如果两者都设置为 NO,则会出现错误“您必须至少选择 first_boolean 或 second_boolean”

最好的方法是什么?

谢谢!

【问题讨论】:

    标签: php forms symfony symfony-forms multiple-choice


    【解决方案1】:

    如果你只有表单类型而没有底层表单类型,你可以添加一个简单的Expression constraint

    use Symfony\Component\Validator\Constraints as Assert;
    
    ....
    
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add("is_my_first_boolean", ChoiceType::class, array(
                "expanded" => true,
                "multiple" => false,
                "choices" => array(
                    'Yes' => "1",
                    'No' => "0"
                ),
                'constraints' => [
                    new Assert\Expression(array(
                        'expression' => 'value == 1 or this.getParent()["is_my_second_boolean"].getData() == 1',
                        'message' => 'Either is_my_first_boolean or is_my_second_boolean must be selected',
                    ))
                ]
            ))
            ->add("is_my_second_boolean", ChoiceType::class, array(
                "expanded" => true,
                "multiple" => false,
                "choices" => array(
                    'Yes' => "1",
                    'No' => "0"
                ),
                'constraints' => [
                    new Assert\Expression(array(
                        'expression' => 'value == 1 or this.getParent()["is_my_first_boolean"].getData() == 1',
                        'message' => 'Either is_my_first_boolean or is_my_second_boolean must be selected',
                    ))
                ]
            ));
    }
    

    注意表达式中的第二个 or 包含对另一个字段的引用。这样,两个字段都得到了“错误”。如果这太多了,您可以只删除一个约束,并且只有一个字段突出显示错误。

    如果您的表单由数据类支持,您当然可以将表达式约束添加到此类:

    /**
     * @Assert\Expression(
     *     "this.getisMyFirstBoolean() or this.getisMySecondBoolean()",
     *     message="Either first or second boolean have to be set",
     * )
     */
     class MyFormData
    

    在这种情况下,错误消息显示在表单级别。

    【讨论】:

    • MMhh,不知道为什么,但是这个断言对我不起作用,我可以在表达式中表达任何内容,它总是可以的
    • 您使用哪个 PHP 版本?您是否尝试过使用value == "1" 而不是value == 1(即与字符串比较)?
    猜你喜欢
    • 2017-01-16
    • 2013-02-15
    • 1970-01-01
    • 2014-12-23
    • 2022-01-03
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    相关资源
    最近更新 更多