Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel validator `required` fails for empty string also

I'm trying laravel required validator in my code, unfortunately it fails for even empty string. I do not want it fail for empty string.

$validator = \Validator::make(array("name"=>""), array("name"=>"required"));
if ($validator->fails()){
    var_dump($validator->messages());
} else {
    die("no errors :)");
}

It gives me the following output

object(Illuminate\Support\MessageBag)[602]
  protected 'messages' => 
    array (size=1)
      'name' => 
        array (size=1)
          0 => string 'The name field is required.' (length=27)
  protected 'format' => string ':message' (length=8)

It is supposed to pass, since i'm giving an empty string as the name field.

The above behavior happens in OSX environment (PHP Version 5.5.18), but it works fine in linux environment (PHP Version 5.5.9-1ubuntu4.5).

like image 922
shahalpk Avatar asked Jan 19 '15 08:01

shahalpk


People also ask

How can I validate my name in laravel?

php", to replace the :attribute name (input name) for a proper to read name (example: first_name > First name) . It seems very simple to use, but the validator doesn't show the "nice names". And the validation in the controller: $validation = Validator::make($input, $rules, $messages);

What is confirmed validation in laravel?

By default, Laravel 'confirmed' validator adds the error message to the original field and not to the field which usually contains the confirmed value.


1 Answers

I use this:

'my_field' => 'present'  
like image 163
Igor Panchenko Avatar answered Nov 15 '22 13:11

Igor Panchenko