简体   繁体   English

Laravel 验证一个字段大于或等于另一个字段不起作用

[英]Laravel validation of one field being greater than or equal to another field not working

I'm using Laravel 5.8.26 and am trying to validate that one field is greater than or equal to another field.我正在使用 Laravel 5.8.26 并试图验证一个字段是否大于或等于另一个字段。

Here's my code:这是我的代码:

'amountMin' => 'required|numeric|gte:0',
'amountMax' => 'required|numeric|gte:amountMin',

the value for amountMin is 5000 and the value for amountMax is 40000. amountMin值为 5000, amountMax值为 40000。

when my code runs I get the message that amountMax must be greater than or equal to amountMin .当我的代码运行时,我收到消息amountMax必须大于或等于amountMin

I found this StackOverflow post that says in order to do this you have to create a custom validation rule, but according to the way I understand Laravel validators to work my code should work.我发现这个StackOverflow 帖子说为了做到这一点你必须创建一个自定义验证规则,但根据我理解 Laravel 验证器工作的方式,我的代码应该可以工作。 Am I wrong, do I need to create a custom validation rule to ensure amountMax is greater than or equal to amountMin or do I just need to adjust my code a bit to get it to work?我错了,我是否需要创建自定义验证规则以确保amountMax大于或等于amountMin还是我只需要稍微调整我的代码即可使其工作?

As always a correct, clearly explained answer will be marked as accepted answer and upvoted.一如既往,正确、清楚解释的答案将被标记为已接受的答案并被点赞。

Thanks in advance.提前致谢。

This should get you there:这应该让你到达那里:

'amountMin' => 'required_with:amountMax|numeric|gte:0',
'amountMax' => 'required_with:amountMin|numeric|gte:amountMin',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM