php 如果 Laravel 中的值不为空,如何验证输入字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43085274/
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 validate an input field if value is not null in Laravel
提问by noufalcep
I am trying to create and update users with laravel 5.4
我正在尝试使用 laravel 5.4 创建和更新用户
This is the validation added for create user. It works.
这是为创建用户添加的验证。有用。
$this->validate($request, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
On update the password field is not required. But validate the min:6 and confirmed rule if the password field is not null. Tried with sometimes
.. but not working..
更新时不需要密码字段。但如果密码字段不为空,则验证 min:6 和 Confirmed 规则。尝试过sometimes
..但不工作..
$this->validate($request, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users,email,'.$id,
'password' => 'sometimes|min:6|confirmed',
]);
回答by Rejinderi
try using nullable as a rule
尝试使用 nullable 作为规则
'password' => 'nullable|min:6|confirmed',
see https://laravel.com/docs/5.8/validation#a-note-on-optional-fields
见https://laravel.com/docs/5.8/validation#a-note-on-optional-fields
回答by C.V
In case column is nullable
如果列可以为空
'password' => 'nullable|min:6|confirmed',
@Rejinderi's answer is correct!
@Rejinderi 的回答是正确的!
In case column is not nullable (This may help other)
如果列不可为空(这可能对其他人有帮助)
'password' => 'sometimes|required|min:6|confirmed',
Result
结果
username = 'admin',
password = null // fail- required
username = 'admin',
password = 123 // fail- min:6
username = 'admin' // pass- validate only exist
回答by linktoahref
Create rules based on the password field. Validate only if the password field is present.
根据密码字段创建规则。仅当密码字段存在时才进行验证。
if ($request->input('password')) {
$rules = [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users,email,'.$id,
'password' => 'required|min:6|confirmed',
];
} else {
$rules = [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users,email,'.$id
];
}
$this->validate($request, $rules);
回答by Jarik Voroniak
This is how I would do it:
这就是我将如何做到的:
//For new or create :
$this->validate($request, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
//For edit or update:
$this->validate($request, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'min:6|confirmed',//just remove required from password rule
]);
Explanation:
This way the value will be validated only when it will be defined (present) in request
If you use nullable, than validator will accept nullas value (which i assume is not acceptable)
If you remove passwordvalidation from update than this input will not be validated at all, and any valuewill be accepted (which is again not acceptable);
说明:
这样的值将只验证时,将在请求中定义(本次)
如果使用可为空的,比验证器将接受空作为值(我假定是不能接受的)
如果删除密码从更新的验证比该输入根本不会被验证,任何值都将被接受(这也是不可接受的);