如何在Laravel 5请求类中使用有时规则

时间:2015-05-18 09:55:48

标签: php laravel laravel-5

我有以下请求类:

<?php namespace App\Http\Requests\User;

use App\Http\Requests\Request;
use Validator;
use Session;
use Auth;
use App\User;

class RegisterStep1Request extends Request {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Set up the validation rules
     */
    public function rules()
    {
        Validator::extend('valid_date', function($attribute, $value, $parameters)
        {
            $pieces = explode('/', $value);
            if(strpos($value, '/')===FALSE) {
                return false;
            } else {
                if(checkdate($pieces[1], $pieces[0], $pieces[2])) {
                    return true;
                } else {
                    return false;
                }
            }
        });

        return [
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required|email|unique:users,email',
            'dob' => 'required|regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/|valid_date',
            'mobile' => 'required',
            'password' => 'required|confirmed'
        ];
    }

    public function messages()
    {
        return [
            'first_name.required' => 'The first name field is required.',
            'last_name.required' => 'The last name field is required.',
            'email.required' => 'The email address field is required.',
            'email.email' => 'The email address specified is not a valid email address.',
            'email.unique' => 'The email address is already registered with this website.',
            'dob.required' => 'The date of birth field is required.',
            'dob.regex' => 'The date of birth is invalid. Please use the following format: DD/MM/YYYY.',
            'dob.valid_date' => 'The date of birth is invalid. Please check and try again.',
            'mobile.required' => 'The mobile number field is required.',
            'password.required' => 'The password field is required.',
            'password.confirmed' => 'The confirm password field does not match the password field.'
        ];
    }

}

我想添加以下有时规则:

Validator::sometimes('dob', 'valid_date', function($input)
{
    return apply_regex($input->dob) === true;
});

如何将此添加到我的请求类中?

我已将我的规则方法修改为以下内容:

public function rules()
{
    Validator::extend('valid_date', function($attribute, $value, $parameters)
    {
        $pieces = explode('/', $value);
        if(strpos($value, '/')===FALSE) {
            return false;
        } else {
            if(checkdate($pieces[1], $pieces[0], $pieces[2])) {
                return true;
            } else {
                return false;
            }
        }
    });

    Validator::sometimes('dob', 'valid_date', function($input)
    {
        return apply_regex($input->dob) === true;
    });

    return [
        'first_name' => 'required',
        'last_name' => 'required',
        'email' => 'required|email|unique:users,email',
        'dob' => 'sometimes|required|regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/|valid_date',
        'mobile' => 'required',
        'password' => 'required|confirmed'
    ];
}

但是,当我提交表单时,我现在收到以下错误:

FatalErrorException in Facade.php line 216:
Call to undefined method Illuminate\Validation\Factory::sometimes()

4 个答案:

答案 0 :(得分:13)

您可以通过覆盖表单请求中的sometimes()函数来附加getValidatorInstance()规则:

protected function getValidatorInstance(){
    $validator = parent::getValidatorInstance();

    $validator->sometimes('dob', 'valid_date', function($input)
    {
        return apply_regex($input->dob) === true;
    });

    return $validator;
}

答案 1 :(得分:12)

在Laravel 5.4中有一个documented way来更改请求的验证程序实例。您应该为此实现withValidator方法。

根据@lukasgeiter's answer中的示例,您可以将以下内容添加到您的请求类中:

/**
 * Configure the validator instance.
 *
 * @param  \Illuminate\Validation\Validator  $validator
 * @return void
 */
public function withValidator($validator)
{
    $validator->sometimes('dob', 'valid_date', function ($data) {
        return apply_regex($input->dob) === true;
    });
}

通过这样做,您不必担心重写内部方法。此外,这似乎是配置验证器的官方方式。

答案 2 :(得分:1)

根据你的评论

  

我希望规则valid_date仅在正则表达式规则返回true时运行。除此以外   如果日期不是正确的格式,则valid_date规则会出错。

Validator::extend('valid_date', function($attribute, $value, $parameters)
    {
       \\use the regex here instead

        if (!preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/', $value)) return false; 


        $pieces = explode('/', $value);
        if(strpos($value, '/')===FALSE) {
            return false;
        } else {
            if(checkdate($pieces[1], $pieces[0], $pieces[2])) {
                return true;
            } else {
                return false;
            }
        }
    });


$validator = Validator::make($data, [
   'first_name' => 'required',
    'last_name' => 'required',
    'email'     => 'required|email|unique:users,email',
    'dob'       => 'required|valid_date',
    'mobile'    => 'required',
    'password'  => 'required|confirmed'
]);

答案 3 :(得分:0)

您只需要将dob键添加到要返回的数组中,并附上要遵循的验证规则集,包括sometimes

在这种情况下:

'dob' : 'sometimes|required|regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/|valid_date'