如何使 Laravel 的 Validator $rules 成为可选?

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

How to make Laravel's Validator $rules optional?

phpvalidationlaravellaravel-4eloquent

提问by intelis

Let's say I have Usermodel with two methods:

假设我有User两种方法的模型:

User.php

用户名.php

class User extends Eloquent
{  
    /* Validation rules */
    private static $rules = array(
        'user'  => 'unique:users|required|alpha_num',
        'email' => 'required|email'
    );

    /* Validate against registration form */
    public static function register($data)
    {
        $validator = Validator::make($data, static::$rules);
        if($validator->fails())
        {
            /*... do someting */
        }
        else
        {
            /* .. do something else */
        }
    }

    /* Validate against update form */
    public static function update($data)
    {
        $validator = Validator::make($data, static::$rules);
        if($validator->fails())
        {
            /*... do someting */
        }
        else
        {
            /* .. do something else */
        }
    }
}

My question: How can I make validation rules optional, so even if data for update()would be just emailfield, it would ignore userand still validate to true.
Is this even possible or am I missing something?

我的问题:我怎样才能使验证规则成为可选的,所以即使数据update()只是email字段,它也会忽略user并仍然验证到true.
这甚至可能还是我错过了什么?

Sorry for my bad English.

对不起,我的英语不好。

回答by Juan Carlos Brown

Not sure if I'm getting your question right but if the user is optional you should remove 'required' from the validator. This way you will have:

不确定我是否正确回答了您的问题,但如果用户是可选的,您应该从验证器中删除“必需”。这样你将拥有:

'user'  => 'unique:users|alpha_num',

instead of:

代替:

'user'  => 'unique:users|required|alpha_num',

On the other hand I create a custom method for my models that is able to return custom validation rules depending on incoming parameters.

另一方面,我为我的模型创建了一个自定义方法,该方法能够根据传入参数返回自定义验证规则。

For example:

例如:

private function getValidationRules($rules)
{
    if ($rules == UPDATE_EMAIL)
    {
        return array('email' => 'required|email');
    } else {
        return array(
            'user'  => 'unique:users|required|alpha_num',
            'email' => 'required|email'
        );
    }
}

I guess it's only a personal choice, but I have found that getting the validation rules from a method allows more control over what I really want to validate, especially when you want to perform some advanced validations.

我想这只是个人选择,但我发现从方法中获取验证规则可以更好地控制我真正想要验证的内容,尤其是当您想要执行一些高级验证时。

Hope it helps you.

希望对你有帮助。

回答by Ben Claar

In stock Laravel, you can call update()on a model, and it won't validate by default, which will give you the desired behavior you described. In the code you posted, you're explicitly overriding the update()method to force validation.

在 Laravel 中,您可以调用update()模型,并且默认情况下它不会验证,这将为您提供您描述的所需行为。在您发布的代码中,您明确覆盖了update()强制验证的方法。

There are two ways to make the "user" field optional in the code you posted:

有两种方法可以使您发布的代码中的“用户”字段成为可选字段:

  1. Don't set "required" in your $rules for that field.

    'user'  => 'unique:users|alpha_num',
    
  2. Don't override the update()method to force validation before updating.

  1. 不要在该字段的 $rules 中设置“必需”。

    'user'  => 'unique:users|alpha_num',
    
  2. update()在更新之前不要覆盖该方法以强制验证。

回答by Joeri

How about:

怎么样:

private static $rules = array(
    'user'  => 'unique:users|required|alpha_num',
    'email' => 'required|email'
);
private static $update_rules = array(
    'user'  => 'required|alpha_num',
    'email' => 'required|email'
);

* Validate against registration form */
public static function register($data)
{
    $validator = Validator::make($data, static::$rules);
    if($validator->fails())
    {
        /*... do someting */
    }
    else
    {
        /* .. do something else */
    }
}

/* Validate against update form */
public static function update($data)
{
    $validator = Validator::make($data, static::$update_rules);
    if($validator->fails())
    {
        /*... do someting */
    }
    else
    {
        /* .. do something else */
    }
}