Laravel 有时验证规则

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

Laravel sometimes validation rule

phpvalidationlaravel

提问by Steven1978

I am trying to validate a password field only if it is present. I want to allow someone to edit a user and they may or may not want to change the users password. So I thought I could this using Laravels validation rules, specifically the 'sometimes' rule. I have this set of rules:

我试图仅在密码字段存在时才对其进行验证。我想允许某人编辑用户,他们可能想也可能不想更改用户密码。所以我想我可以使用 Laravel 验证规则,特别是“有时”规则。我有这组规则:

$this->validate($request, [
    'password' => 'sometimes|required|min:8',
]);

This is simplified for the example, there will usually be other rules for other fields and stricter rules for the password. I expect this to only apply the min:8 rule if the password field is present in the passed data, but if I leave the password field empty I get a validation error saying the password field is required.

这对于示例来说是简化的,通常会有其他字段的其他规则和更严格的密码规则。我希望这仅在传递的数据中存在密码字段时应用 min:8 规则,但如果我将密码字段留空,我会收到验证错误,提示密码字段是必需的。

I'm not sure what I'm not understanding in the docs. Do I need to manually remove the password field before validation if it is the form input was submitted empty like this?

我不确定我在文档中不明白什么。如果表单输入像这样提交为空,我是否需要在验证之前手动删除密码字段?

$data = $request->all();

if ('' === $data['password']) {
    unset($data['password'])
}

...and then pass the array into the validator. I think this makes sense but I could do with some confirmation that I'm understanding it correctly. Thanks in advance.

...然后将数组传递给验证器。我认为这是有道理的,但我可以确认我正确理解它。提前致谢。

回答by Clean code

I think we should tell laravel If password is not empty put the rules otherwise do nothing.

我觉得应该告诉laravel如果密码不为空就把规则放否则什么都不做。

$this->validate($request, [
  'password' => $request->password != null ?'sometimes|required|min:8': ''
]);

回答by Nabin Kunwar

Docs don't make it clear, But removing required makes it work.

文档没有说清楚,但删除 required 使它工作。

$this->validate($request, [
    'password' => 'sometimes|min:8',
]);

回答by AlmostPitt

"I am trying to validate a password field only if it is present."

“我试图仅在密码字段存在时才对其进行验证。”

To do this, you can use the "nullable" rule within the validation system.

为此,您可以在验证系统中使用“可为空”规则。

For example:

例如:

$this->validate($request, [
    'password' => 'sometimes|nullable|min:8',
]);

For more information take a look at https://laravel.com/docs/5.6/validationsection under "A Note On Optional Fields"

有关更多信息,请查看“关于可选字段的说明”下的https://laravel.com/docs/5.6/validation部分

回答by kitensei

I think it's generally safer to allow the user to change its password only if he can provided the old one.

我认为只有在用户可以提供旧密码的情况下才允许用户更改密码通常更安全。

Allowing the connected user to alter his password without providing the old one can be a security issue.

允许连接的用户在不提供旧密码的情况下更改他的密码可能是一个安全问题。

This is generally how I allow user password change with Laravel:

这通常是我允许使用 Laravel 更改用户密码的方式:

 $this->validate($request, [
            'user.old_password' => [],
            'user.password' => [
                'required_with:user.old_password',
                'min:6',
                'confirmed',
                'regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*([-+_!@#$%^&*.,;?])).+$/',
                'different:user.old_password'
            ],
            'user.password_confirmation' => ['required_with:user.password'],
        ]);

This don't validate the old password as we don't care, the database will check it for us, but I validate the new password only if the old one is provided.

这不会验证旧密码,因为我们不在乎,数据库会为我们检查它,但我仅在提供旧密码时验证新密码。

回答by paranoid

In edit mode you fill password field by for example "********" and in update mode validate like this

在编辑模式下,你填写密码字段,例如“********”,在更新模式下像这样验证

$this->validate($request, [
    'password' => 'required|min:8',
]);

and in controller check $data['password']='********'find old password and

并在控制器检查中$data['password']='********'找到旧密码和

$data['password']='old password in db'

and $data['password']!='********'update pssword

$data['password']!='********'更新 pssword

回答by tvpeter

As an option to the selected answer, you can use:

作为所选答案的选项,您可以使用:

    $this->validate($request, [
       'password' => 'nullable|min:8',
    ]);

回答by tvpeter

Laravel sometimes validation rule and their function:

Laravel 有时会验证规则及其功能:

acceptedThe field under validation must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance.

接受验证中的字段必须是 yes、on、1 或 true。这对于验证“服务条款”的接受度很有用。

active_urlThe field under validation must have a valid A or AAAA record according to the dns_get_record PHP function.

active_url根据 dns_get_record PHP 函数,验证中的字段必须具有有效的 A 或 AAAA 记录。

after:dateThe field under validation must be a value after a given date. The dates will be passed into the strtotime PHP function: 'start_date' => 'required|date|after:tomorrow' Instead of passing a date string to be evaluated by strtotime, you may specify another field to compare against the date: 'finish_date' => 'required|date|after:start_date'

after:date验证字段必须是给定日期之后的值。日期将被传递到 strtotime PHP 函数中: 'start_date' => 'required|date|after:tomorrow' 代替传递由 strtotime 计算的日期字符串,您可以指定另一个字段来与日期进行比较:'finish_date ' => '必需|日期|之后:开始日期'

after_or_equal:dateThe field under validation must be a value after or equal to the given date. For more information, see the after rule.

after_or_equal:date验证字段必须是给定日期之后或等于给定日期的值。有关更多信息,请参阅 after 规则。

alphaThe field under validation must be entirely alphabetic characters.

alpha验证字段必须完全是字母字符。