我有以下请求类:

<?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;
});

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

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

但我现在在提交表格时出现以下错误:

FatalErrorException in Facade.php line 216:
Call to undefined method Illuminate\Validation\Factory::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相关问答推荐

Laravel模型嵌入嵌套模型时只附加属性

子目录中具有Vue实例的Laravel不起作用

Laravel | 使用select查询

找不到 Laravel 5 类日志(log)

RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException

错误try 访问 null 类型值的数组偏移量laravel 5.8.26 Php 7.4.2

Laravel 4:处理种子中的关系

Laravel 5.3 - 将多个文件附加到 Mailables

如何判断是否在laravel中设置了cookie?

Laravel 表 * 没有名为 * 的列

Laravel - 批量分配异常错误

Laravel 同步错误

Laravel - 更新时禁用更新时间

命令未定义异常

Laravel 5.3 通知 - 仅使用Electron邮件地址通知

如何在 Laravel 中正确安装软件包?

将 Laravel 集合排序为 ID 数组

Laravel homestead IP地址不起作用

Laravel - 使用 Eloquent 查询构建器在 Select 中添加自定义列

限制自己重载外部 API 的速率