我想比较表格SYMFONY4上的两个属性

我有这个表单类型:


    public function buildForm(FormBuilderInterface $builder, array $options)

    {

        $builder

            ->add('ageMin', NumberType::class),

            ->add('ageMax', NumberType::class,[

'mapped' => false

]);

    }

我的问题是,我想在提交表单之前验证此表达式:ageMin <= ageMax


我已经尝试过此代码,但是不起作用


    $resolver->setDefaults([

            'data_class' => User::class,

            'constraints' => [

new Assert\Expression([

                'expression' => 'value["ageMax] >= value["ageMin"]'

            ]),

]

        ]);

如果还有其他解决方案?


撒科打诨
浏览 158回答 3
3回答

万千封印

&nbsp;'constraints' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Assert\Expression([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'expression' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (this["ageMin"].getData() <= this["ageMax"].getData() )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'message' => ''&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]),

冉冉说

您可以使用以下任一方法轻松解决此问题约束表达自定义验证约束
打开App,查看更多内容
随时随地看视频慕课网APP