如何访问 Laravel 中的验证错误 属性 名称?
how to access validation error property name in Laravel?
我从 Laravel 开始,我尝试对其进行验证并查看每个输入的错误,我已经遍历 $error->all()
但我需要在其下显示每个输入的错误并且我使用此代码 $errors->has('password')
来显示输入有错误,但我也需要显示消息。
我得到 print_r($errors)
并且它 return 这个:
Illuminate\Support\ViewErrorBag Object
(
[bags:protected] => Array
(
[default] => Illuminate\Support\MessageBag Object
(
[messages:protected] => Array
(
[firstName] => Array
(
[0] => The first name field is required.
)
[lastName] => Array
(
[0] => The last name field is required.
)
[email] => Array
(
[0] => The email must be a valid email address.
[1] => The email field is required.
)
[password] => Array
(
[0] => The password field is required.
)
[password_confirmation] => Array
(
[0] => The password confirmation field is required.
)
)
[format:protected] => :message
)
)
)
我的问题是我无法访问消息?
AS 根据 Laravel 文档,您可以使用 @error
指令实现相同的
The $errors
variable is bound to the view by the
Illuminate\View\Middleware\ShareErrorsFromSession middleware
, which is
provided by the web middleware
group. When this middleware is applied
an $errors variable will always be available in your views, allowing
you to conveniently assume the $errors variable is always defined and
can be safely used.
例如
<label for="title">Post Title</label>
<input id="title" type="text" class="@error('title') is-invalid @enderror">
@error('title')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
我从 Laravel 开始,我尝试对其进行验证并查看每个输入的错误,我已经遍历 $error->all()
但我需要在其下显示每个输入的错误并且我使用此代码 $errors->has('password')
来显示输入有错误,但我也需要显示消息。
我得到 print_r($errors)
并且它 return 这个:
Illuminate\Support\ViewErrorBag Object
(
[bags:protected] => Array
(
[default] => Illuminate\Support\MessageBag Object
(
[messages:protected] => Array
(
[firstName] => Array
(
[0] => The first name field is required.
)
[lastName] => Array
(
[0] => The last name field is required.
)
[email] => Array
(
[0] => The email must be a valid email address.
[1] => The email field is required.
)
[password] => Array
(
[0] => The password field is required.
)
[password_confirmation] => Array
(
[0] => The password confirmation field is required.
)
)
[format:protected] => :message
)
)
)
我的问题是我无法访问消息?
AS 根据 Laravel 文档,您可以使用 @error
指令实现相同的
The
$errors
variable is bound to the view by theIlluminate\View\Middleware\ShareErrorsFromSession middleware
, which is provided by the webmiddleware
group. When this middleware is applied an $errors variable will always be available in your views, allowing you to conveniently assume the $errors variable is always defined and can be safely used.
例如
<label for="title">Post Title</label>
<input id="title" type="text" class="@error('title') is-invalid @enderror">
@error('title')
<div class="alert alert-danger">{{ $message }}</div>
@enderror