I am not able to display the validation errors on a specific form/page when using $request->validate($rules);
. Here is the controller code:
public function store(Request $request) {
$rules = [
'dummy-field' => 'required|string',
];
$params = $request->validate($rules);
// $this->validate($request, $rules); // Same result with this
dd($params);
}
In my view:
@if($errors->any())
<div class="alert alert-danger">
@foreach($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
</div>
@endif
When I submit the form (without the dummy-field
) I'm properly redirected back to the previous page (If my form would pass the validation I should see the dd
output), but the error messages are not displayed. I also tried dd($errors)
and the ErrorBag is actually empty.
The weird thing is, using a manual Validator the error messages are properly displayed. Refactoring the controller to:
$rules = [
'dummy-field' => 'required|string',
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator);
}
dd($validator);
I see the error messages.
I really can't understand this behaviour. I'm using $request->validate()
for other forms in my app and I don't have any trouble.
I'm using laravel 8.
Does someone have any idea why something like this could happen?
question from:
https://stackoverflow.com/questions/65905616/laravel-does-not-display-validation-errors-when-using-request-validate 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…