Laravel validator `required` fails for empty string also
I use this:
'my_field' => 'present'
The required
rule actually returns false if you pass an empty string.
If we look at the code (Illuminate\Validation\Validator
)
protected function validateRequired($attribute, $value)
{
if (is_null($value))
{
return false;
}
elseif (is_string($value) && trim($value) === '')
{
return false;
}
// [...]
return true;
}
I think your only option here is to write your own validation rule that checks if the value is not null:
Validator::extendImplicit('attribute_exists', function($attribute, $value, $parameters){
return ! is_null($value);
});
(The extendImplicit
is needed because with extend
custom rules will only run when the value is not an empty string)
And then use it like this:
\Validator::make(array("name"=>""), array("name"=>"attribute_exists"));