How to make Laravel's Validator $rules optional?

痴心易碎 提交于 2019-12-04 01:46:57

问题


Let's say I have User model with two methods:

User.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 email field, it would ignore user and still validate to true.
Is this even possible or am I missing something?

Sorry for my bad English.


回答1:


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.




回答2:


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.

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.




回答3:


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 */
    }
}


来源:https://stackoverflow.com/questions/18447404/how-to-make-laravels-validator-rules-optional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!