Back

Questions about "Validator::extendImplicit".


Questions 1:

In the official manual of Laravel I saw:

By default, when an attribute being validated is not present or contains an empty value as defined by the required rule, normal validation rules, including custom extensions, are not run. For example, the unique rule will not be run against a null value:

https://laravel.com/docs/5.6/validation#Implicit Extensions

$rules = ['name' => 'unique'];

$input = ['name' => null];

Validator::make($input, $rules)->passes(); // true

But I saw that unique's rules are written like this:The field under validation must be unique in a given database table.

It is not an error in the official manual example, and I personally tried it and I did report an error.


Questions 2:

The manual says:By default, when an attribute being validated is not present or contains an empty value as defined by the required rule, normal validation rules, including custom extensions, are not run.

Here is an example:

$rules = ['name' => 'unique:users'];
$input = ['name' =>null];
$b = Validator::make($input, $rules)->passes();
dd($b);

Then I deliberately add a NULL value to the name field in the users data table.

The result returns false.

Not the same as the manual

Sorry, my English is not very good, I hope to describe my problem clearly.

Pardeeptech replied 1 week ago

you may be use like this to validate By default, when an attribute being validated is not present or contains an empty value as defined by the required rule, normal validation rules, including custom extensions, are not run. For example, the unique rule will not be run against a null value:

$rules = ['name' => 'unique'];

$input = ['name' => null];

Validator::make($input, $rules)->passes(); // true
For a rule to run even when an attribute is empty, the rule must imply that the attribute is required. To create such an "implicit" extension, use the Validator::extendImplicit() method:

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

more information : https://laravel.com/docs/5.6/validation

Nick replied 1 week ago

what are you doing? What is the use of copying the contents of the manual? If you can't help me, please don't reply to these worthless answers.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us