在 Laravel 中自定义模型存储的验证规则

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

Customizing model-stored validation rules in Laravel

phpvalidationmodellaravel

提问by arnisritins

Let's say, I have a User model in Laravel like this:

比方说,我在 Laravel 中有一个 User 模型,如下所示:

class User extends Eloquent implements UserInterface, RemindableInterface {

    public static $rules = array(
        'email' => 'required|email',
        'password' => 'required|min:8|confirmed',
        'password_confirmation' => 'required|min:8'
    );

    ...

}

Rules, stored in model, will be reused both for login and register forms but problem occurs when there's no need for password confirmation (e.g. login form). And there could be many such situations where the rules should be changed.

存储在模型中的规则将在登录和注册表单中重复使用,但是当不需要密码确认(例如登录表单)时会出现问题。可能有很多这样的情况应该改变规则。

So, is there any pure method how to modify model-stored validation rules for different cases in Laravel? Do I have to reorganize my rule storage approach at all?

那么,是否有任何纯方法如何在 Laravel 中针对不同情况修改模型存储的验证规则?我是否必须重新组织我的规则存储方法?

Thanks!

谢谢!

回答by Anam

You can add rules dynamically when you need them.

您可以在需要时动态添加规则。

For example:

例如:

If I am right, you only need the password_confirmationrule when registering a user and when updating a password. So, in your model, do not add the password_confirmationrule .

如果我是对的,您只需要password_confirmation在注册用户和更新密码时使用规则。因此,在您的模型中,不要添加password_confirmation规则。

public static $rules = array(
        'email' => 'required|email',
        'password' => 'required|min:8|confirmed'

}

How to add the rule dynamically:

如何动态添加规则:

To register a user, the password_confirmationfield is required. So, from your controller, you can always add rules like the following:

要注册用户,该password_confirmation字段是必需的。因此,从您的控制器中,您始终可以添加如下规则:

$rules = User::$rules;

$rules['password_confirmation'] = 'required|min:8';

and sometimes you may need to add rules based on user input.

有时您可能需要根据用户输入添加规则。

For example:

例如:

If a user selects Australiaas country, they must also select a state.

如果用户选择Australia国家,他们还必须选择一个州。

$v = Validator::make($data, $rules ));
$v->sometimes('state', 'required', function($input)
{
   return $input->country == 'Australia';
});

回答by Phil T.

Late to the game but per Laravel docs you can use a 'sometimes' rule.

游戏较晚,但根据 Laravel 文档,您可以使用“有时”规则。

http://laravel.com/docs/validation

http://laravel.com/docs/validation

In brief: In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list:

简而言之:在某些情况下,您可能希望仅当该字段存在于输入数组中时才对该字段运行验证检查。要快速完成此操作,请将有时规则添加到您的规则列表中:

    'password_confirmation' => 'sometimes|required|min:8|confirmed'

回答by AZReed

I do something like this.

我做这样的事情。

In the Model:

在模型中:

public static $rules = [
    'create' => [
        'first_name' => 'min:3',
        'last_name'  => 'min:3',
        'email'      => 'required|email|unique:users',
        'password'   => 'required|min:5|confirmed'
        ],
    'edit'   => [
        'first_name' => 'other',
        'last_name'  => 'other',
        'email'      => 'other',
        'password'   => 'other|min:5'
        ]
     ];

In the Controller:

在控制器中:

$validator = Validator::make( $input, User::$rules['edit'] ); # Or User::$rules['create']

if ( $validator->fails() ) { code }

回答by omarjebari

As far as i can see the Ardent Package handles model validation well for Laravel > 5.0. It appears to supoort all of the built-in validation features (such as the use of 'sometimes' when you only want to validate a field if it's passed) as well as extending them.

据我所知,Ardent Package 可以很好地处理 Laravel > 5.0 的模型验证。它似乎支持所有内置验证功能(例如,当您只想验证通过的字段时使用“有时”)以及扩展它们。

http://packalyst.com/packages/package/laravelbook/ardent

http://packalyst.com/packages/package/laravelbook/ardent