如何在 Laravel 中使用 required_unless
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37342037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use required_unless in laravel
提问by Chintan7027
I have gone through the validations in Laravel. I have taken so many validation rules from Laravel Validations Rules
我已经在 Laravel 中进行了验证。我已经从Laravel 验证规则中获取了很多验证规则
I want to user required_unlessrule for following conditions.
我想 针对以下条件使用required_unless规则。
$rules = array(
'facebookID' => 'alpha_num',
'googleID' => 'alpha_num',
'email' => 'required_unless:facebookID,null|required_unless:facebookID,null|email|max:32',
'password' => 'required_unless:facebookID,""|required_unless:googleID,""|max:20',
);
I want to add validation rule of email and passwordis only requiredif signup with facebook or google is not attempted.
我想添加电子邮件和密码的验证规则,只有在未尝试使用 facebook 或 google 注册时才需要。
I believe we can use required_unlessor required_if
我相信我们可以使用required_unless或required_if
Please help me to solve this problem.
请帮我解决这个问题。
回答by Dat Pham
The laravel validation rule only accept value, that's mean required_unless:facebookID,null
is evaluated as: the field is required unless facebookID='null'. So it's not a case you would need.
Laravel 验证规则只接受值,这意味着required_unless:facebookID,null
被评估为:该字段是必需的,除非 facebookID='null'。所以这不是你需要的情况。
My suggest solution is using require_without_all:
我建议的解决方案是使用 require_without_all:
'email' => 'required_without_all:facebookID,googleID',
'password' => 'required_without_all:facebookID,googleID'
The reference link for you: https://laravel.com/docs/5.1/validation#rule-required-without-all
给你的参考链接:https: //laravel.com/docs/5.1/validation#rule-required-without-all
Regards
问候