尝试实现 Laravel 基于长度的验证时
'password' => array(
'required',
'alpha_dash',
'Min:7'
)
并在我的视图中输出错误消息
{{
$errors->first(
'password',
'<span class="error">:message</span>'
)
}}
我明白了
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()
调试,看来是真的。如果我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
)
您可以看到,messages 实际上包含一个密码数组,该数组似乎取决于输入类型,即使我在规则中指定它是 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.
)
)
而其余的,不要
[email] => Array (
[0] => The email format is invalid.
)
在messages.php
Laravel 框架中,它无法处理这种基于数组的消息传递,所以我假设我在它到达那里之前做错了什么,但我不知道是什么。
谢谢你的帮助。