How do you send validation errors to a blade view using the Validator facade? Error: Serialization of 'Closure' is not allowed
How do you send validation errors to a blade view using the Validator facade? Error: Serialization of 'Closure' is not allowed
如何在 Laravel 9 中使用 validateWithBag90
?我尝试了很多组合,但我有点沮丧。
$validator = Validator::make($request->all(), [
'old' => 'required',
'new' => 'required|string|confirmed',
]);
if ($validator->fails()) {
return redirect()->back()->with('error', $validator);
}
我遇到了这个错误。
Serialization of 'Closure' is not allowed
有什么想法吗?
After determining whether the request validation failed, you may use
the withErrors
method to flash the error messages to the session. When
using this method, the $errors
variable will automatically be shared
with your views after redirection, allowing you to easily display them
back to the user. The withErrors
method accepts a validator, a
MessageBag
, or a PHP array
.
而不是:
// ...
->with('error', $validator); ❌
// ...
使用这个:
// ...
if ($validator->fails()) {
return redirect()
->back()
->withErrors($validator) ✅
->withInput();
}
// ...
附录
Displaying The Validation Errors
An $errors
variable is shared with all of your application's views
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. The $errors
variable will be an
instance of Illuminate\Support\MessageBag
.
<!-- /resources/views/post/create.blade.php -->
<h1>Create Post</h1>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<!-- Create Post Form -->
如何在 Laravel 9 中使用 validateWithBag90
?我尝试了很多组合,但我有点沮丧。
$validator = Validator::make($request->all(), [
'old' => 'required',
'new' => 'required|string|confirmed',
]);
if ($validator->fails()) {
return redirect()->back()->with('error', $validator);
}
我遇到了这个错误。
Serialization of 'Closure' is not allowed
有什么想法吗?
After determining whether the request validation failed, you may use the
withErrors
method to flash the error messages to the session. When using this method, the$errors
variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user. ThewithErrors
method accepts a validator, aMessageBag
, or a PHParray
.
而不是:
// ...
->with('error', $validator); ❌
// ...
使用这个:
// ...
if ($validator->fails()) {
return redirect()
->back()
->withErrors($validator) ✅
->withInput();
}
// ...
附录
Displaying The Validation Errors
An
$errors
variable is shared with all of your application's views by theIlluminate\View\Middleware\ShareErrorsFromSession
middleware, which is provided by theweb
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. The$errors
variable will be an instance ofIlluminate\Support\MessageBag
.
<!-- /resources/views/post/create.blade.php -->
<h1>Create Post</h1>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<!-- Create Post Form -->