0

I'm trying to add a custom rule for validator, but it's not working at all, the function is not even called. I took this from documentation(https://laravel.com/docs/5.4/validation#custom-validation-rules):

In AppServiceProvider::boot I have this:

Validator::extend('foo', function ($attribute, $value, $parameters, $validator) {
   return false;
});

and In my controller I have this:

$validator = Validator::make($request->all(), [
    'myField' => 'foo',
]);

The validator doesn't fail. What am I doing wrong?

4

1 回答 1

2

解决它。我的 json 错误,如果输入值为 null(或为空),则不会检查自定义 Validator::extend 规则。所以解决方案是

Validator::extendImplicit('foo', function ($attribute, $value, $parameters, $validator) {
    return $value == 'foo';
});

或不为空的 $request 字段

于 2017-02-05T02:54:48.353 回答