php Laravel:表单验证字符串长度错误消息导致异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15463903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:17:12  来源:igfitidea点击:

Laravel: Form validation string-length error messages cause exception

phpvalidationlaravellaravel-3

提问by domwrap

When trying to implement Laravel's length based validation

尝试实现 Laravel 基于长度的验证时

'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 );

调试了一下,好像是真的。如果我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

您可以看到消息实际上包含一个似乎取决于输入类型的密码数组,即使我在规则中指定它是 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.phpin 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.

看着messages.php在Laravel框架,它没有任何处理这种基于阵列的消息,所以我认为我做的事情错了,它到那里之前,但我不知道是什么。

Thanks for your help.

谢谢你的帮助。

回答by Mirko Akov

Your rules entry is wrong. It has to be like

您的规则条目是错误的。它必须像

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

Look at the Laravel validation docsfor more information

查看 Laravel 验证文档以获取更多信息

回答by Waqas Ahmed

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

'password.min' => "密码不能少于 6 个字符。",

$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);