Laravel 5.4 验证规则 - 可选,但如果存在则验证;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42319050/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:21:12  来源:igfitidea点击:

Laravel 5.4 validation rules - optional, but validated if present;

laravellaravel-5

提问by GTMeteor

I'm trying to create a user update validation through form, where I pass, for example 'password'=>NULL, or 'password'=>'newone';

我正在尝试通过表单创建用户更新验证,我在其中传递,例如 'password'=>NULL,或 'password'=>'newone';

I'm trying to make it validate ONLY if it's passed as not null, and nothing, not even 'sometimes' works :/

我试图让它仅在它作为非空传递时才进行验证,并且什么都没有,甚至“有时”都不起作用:/

I'm trying to validate as :

我正在尝试验证为:

Validator::make(array('test'=>NULL), array('test'=> 'sometimes|required|min:6'))->validate();

But it fails to validate... :/ ANy help would be appreciated.

但它无法验证...:/任何帮助将不胜感激。

回答by Matt K

Perhaps you were looking for 'nullable'?

也许您正在寻找'nullable'

'test'=> 'nullable|min:6'

回答by nixxx

Though the question is a bit old, this is how you should do it. You dont need to struggle so hard, with so much code, on something this simple.

虽然这个问题有点老,但这就是你应该怎么做。你不需要在这么简单的事情上用这么多代码努力奋斗。

You need to have both nullable and sometimes on the validation rule, like:

您需要同时具有可空性,有时还需要在验证规则上,例如:

$this->validate($request, [
  'username' => 'required|unique:login',
  'password' => 'sometimes|nullable|between:8,20'
]);

The above will validate only if the field has some value, and ignore if there is none, or if it passes null. This works well.

以上仅在该字段具有某个值时才进行验证,如果没有,或者它通过 null,则忽略。这很好用。

回答by HG Sim

回答by sumit

Do not pass 'required' on validator

不要在验证器上传递“必需”

Validate like below

验证如下

$this->validate($request, [
    'username' => 'required|unique:login',
    'password' => 'between:8,20'
]);

The above validator will accept password only if they are present but should be between 8 and 20

上述验证器仅在存在密码时才接受密码,但密码应在 8 到 20 之间

This is what I did in my use case

这就是我在用例中所做的

case 'update':
                $rules = [
                            'protocol_id' => 'required',
                            'name' => 'required|max:30|unique:tenant.trackers'.',name,' . $id, 
                            'ip'=>'required',
                            'imei' => 'max:30|unique:tenant.trackers'.',imei,' . $id, 
                            'simcard_no' => 'between:8,15|unique:tenant.trackers'.',simcard_no,' . $id, 

                            'data_retention_period'=>'required|integer'
                         ];  
            break;

Here the tracker may or may not have sim card number , if present it will be 8 to 15 characters wrong

这里的追踪器可能有也可能没有sim卡号,如果有的话它会是8到15个字符错误

Update

更新

if you still want to pass hardcoded 'NULL' value then add the following in validator

如果您仍然想传递硬编码的“NULL”值,则在验证器中添加以下内容

$str='NULL';
$rules = [
    password => 'required|not_in:'.$str,
];

回答by TecBeast

The relevant validation rules are:

相关的验证规则是:

  • required
  • sometimes
  • nullable
  • 必需的
  • 有时
  • 可空的

All have their uses and they can be checked here:

都有它们的用途,可以在这里检查:

https://laravel.com/docs/5.8/validation#rule-required

https://laravel.com/docs/5.8/validation#rule-required

if you want validation to always apply

如果您希望验证始终适用

https://laravel.com/docs/5.8/validation#conditionally-adding-rules

https://laravel.com/docs/5.8/validation#conditionally-adding-rules

if you want to apply validation rules sometimes

如果您有时想应用验证规则

https://laravel.com/docs/5.8/validation#a-note-on-optional-fields

https://laravel.com/docs/5.8/validation#a-note-on-optional-fields

if you want your attribute to allow for nullas value too

如果您希望您的属性也允许 null作为值

回答by lewis4u

If you are sending null on purpose then make an if condition for validation:

如果您是故意发送 null 则创建一个 if 条件进行验证:

if ($request->test != null)
    Validator::make(array('test' => 'min:6'))->validate();
}