Monday, April 10, 2017

Laravel does not run validator on empty string

I have custom validator.

Validator::extend('custom_validator', function($attribute, $value, $parameters, $validator) {
         return $value === '1';
});

And then how I validate

$v = Validator::make(['test' => 'a'], ['test' => 'custom_validator']);
$v->errors()->all(); // returns [
     "validation.custom_validator",
   ]

But when I pass string which contains only spaces

$v = Validator::make(['test' => '     '], ['test' => 'custom_validator']);
$v->errors()->all(); // returns [], so validator passed what is wrong

So why validator does not run my custom validation function for this string?



via Jackson J

Advertisement