Laravel: Form validation string-length error messages cause exception
Asked Answered
B

2

7

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.

Beverlybevers answered 17/3, 2013 at 17:35 Comment(0)
M
24

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

Metternich answered 18/3, 2013 at 0:48 Comment(2)
Wow, now I feel dumb. I'd used the pipe-separated syntax several times to no avail but it seems the upper-case M on Min was the cause. Thank you for the help. It's a shame those docs don't mention anywhere that rules must be lower-case.Beverlybevers
@Hwulex Don't worry, it's not you, everybody in my team has the same problem, the Laravel docs are really shit when it comes to this!Pitcher
E
2

'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);
Empiric answered 28/5, 2020 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.