Display validation error for multiple forms on the same page
I have to implement two forms on the same page for profile updates on the Laravel Basic Admin panel.
One form for basic info and another for password updates. For validation, used $request->validate() a method in the controller
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.\Auth::user()->id],
]);
In view used the below code to display the errors.
@if ($errors->any())
<ul class="mt-3 list-none list-inside text-sm text-red-400">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
But it displayed errors on both forms because the same $errors variable was used to display errors.
The $errors the variable will be an instance of Illuminate\Support\MessageBag. To resolve the issue, Laravel has the inbuild solution. We can add the names to MessageBag. Laravel is Named Error Bags
Named Error Bags
We can add named errors in multiple ways.
- 1. Redirect withErrors
- 2. validateWithBag method
1. Redirect withErrors
To add a name to the error bag, just pass a name as the second argument to withErrors
For the account added the ‘account’ as a named error and ‘password’ for the password update form.
$validator = Validator::make($request->all(), [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.\Auth::user()->id],
]);
if ($validator->fails()) {
return redirect('admin.account.info')
->withErrors($validator, 'account')
->withInput();
}
Update the below code for views error display
How to show named error bag on view
@if ($errors->account->any())
<ul class="mt-3 list-none list-inside text-sm text-red-400">
@foreach ($errors->account->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
2. validateWithBag method
We can use the validateWithBag method to store the error messages in a named error bag if validation fails:
\Validator::make($request->all(), [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.\Auth::user()->id],
])->validateWithBag('account');
Also, we can use $request->validateWithBag
$request->validateWithBag('account', [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.\Auth::user()->id],
]);
Added below code to validate the password
$validator = \Validator::make($request->all(), [
'old_password' => ['required'],
'new_password' => ['required', Rules\Password::defaults()],
'confirm_password' => ['required', 'same:new_password', Rules\Password::defaults()],
]);
$validator->after(function ($validator) use ($request) {
if ($validator->failed()) return;
if (! Hash::check($request->input('old_password'), \Auth::user()->password)) {
$validator->errors()->add(
'old_password', 'Old password is incorrect.'
);
}
});
$validator->validateWithBag('password');
Laravel validation for multiple forms on the same page
Thank you for reading!
Follow me at balajidharma.medium.com.
Add your feedback and comment If missed any other ways to display an error message for multiple forms.
Top comments (0)