phpvalidationlaravellaravel-3

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.


Solution

  • 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