在Laravel 5请求类中如何使用有时规则 [英] How to use sometimes rule in Laravel 5 request class

查看:106
本文介绍了在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.'
        ];
    }

}

我有时要添加以下规则:

I want to add the following sometimes rule:

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

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

How would I add this to my request class?

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

I have amended my rules method to the following:

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'
    ];
}

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

But I now get the following error when I submit the form:

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

推荐答案

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

You can attach a sometimes() rule by overriding the getValidatorInstance() function in your form request:

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

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

    return $validator;
}

这篇关于在Laravel 5请求类中如何使用有时规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆