Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Form validation string-length error messages cause exception

When trying to implement Laravel's length based validation

'password' => array(
    'required',
    'alpha_dash',
    'Min:7'
)

and outputting error messages in my view

{{
    $errors->first(
        'password',
        '<span class="error">:message</span>'
    )
}}

I get

Unhandled Exception
Message:
Array to string conversion
Location:
_avalog\laravel\messages.php on line 188
Stack Trace:
#0 _avalog\laravel\laravel.php(42): Laravel\Error::native(8, 'Array to string...', '_avalog...', 188)
#1 [internal function]: Laravel\{closure}(8, 'Array to string...', '_avalog...', 188, Array)
#2 _avalog\laravel\messages.php(188): str_replace(':message', Array, 'get('password', 'get()

Debugging, it appears to be true. If I print_r( $validation );

Laravel\Validator Object (
    [attributes] => Array (
        [username] => fred
        [email] =>
        [password] => asd
        [csrf_token] => DWg3CUfqtMZkIRfyZXNEqygvWUHsGS9SQMue2V4S
    )
    [errors] => Laravel\Messages Object (
        [messages] => Array (
            [email] => Array (
                [0] => The email field is required.
            )
            [password] => Array ( 
                [0] => Array (
                    [numeric] => The password must be at least 7.
                    [file] => The password must be at least 7 kilobytes.
                    [string] => The password must be at least 7 characters.
                )
            )
    )
    [format] => :message
)

You can see that messages does in fact contain an array for password which appears to be dependent upon input type, even though I've specified in the rule it is alphadash

[password] => Array (
    [0] => Array (
        [numeric] => The password must be at least 7.
        [file] => The password must be at least 7 kilobytes.
        [string] => The password must be at least 7 characters.
    )
)

Whereas the rest, do not

[email] => Array (
    [0] => The email format is invalid.
)

Looking at messages.php in Laravel framework, it has nothing to handle such array-based messaging so I assume I am doing something wrong before it gets there, but I don't know what.

Thanks for your help.

like image 789
domwrap Avatar asked Mar 17 '13 17:03

domwrap


2 Answers

Your rules entry is wrong. It has to be like

'password' => 'required|alpha_dash|min:7'

Look at the Laravel validation docs for more information

like image 71
Mirko Akov Avatar answered Oct 19 '22 23:10

Mirko Akov


'password.min' => "Password can not be less than 6 characters.",

$custom_validation_messages = array(
  'password.min' => "Password can not be less than 6 characters.",
  'password.required' => "Password is required"
);

   $validator = Validator::make($data, [
                //'email'          => ['required','unique:users,email','email','max:255'],
   'email'          => ['required','email','max:255'],
   'password'       => 'required|min:6|confirmed'

 ],$custom_validation_messages);
like image 33
Waqas Ghouri Avatar answered Oct 19 '22 23:10

Waqas Ghouri