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.

domwrap
  • 443
  • 1
  • 4
  • 12

2 Answers2

23

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

Mirko Akov
  • 2,357
  • 19
  • 19
  • 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. – domwrap Mar 18 '13 at 03:18
  • 1
    @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! – Sliq Apr 05 '16 at 12:40
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);
Waqas Ghouri
  • 1,079
  • 2
  • 16
  • 36