Laravel validation for multiple forms on the same page
Tags: laravel, laravelvalidation, laravelframework, php
Published: 2022-05-19 02:13:39

---
title: Laravel validation for multiple forms on the same page
published: true
date: 2022-05-08 19:04:32 UTC
tags: laravel,laravelvalidation,laravelframework,php
canonical_url: https://blog.devgenius.io/laravel-validation-for-multiple-forms-on-the-same-page-74ad420a5e6
---

#### Display validation error for multiple forms on the same page

![Display validation error for multiple forms on the same page](https://cdn-images-1.medium.com/max/1024/0*VayejMEMt8acxB1B)
_Photo by Max Chen on Unsplash_

I have to implement two forms on the same page for profile updates on the [Laravel Basic Admin panel](https://github.com/balajidharma/basic-laravel-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.

![Laravel display errors](https://cdn-images-1.medium.com/max/837/1*7TnGI6f40MMhxHeUKkOrvw.png)

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](https://laravel.com/docs/9.x/validation#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](https://laravel.com/docs/9.x/validation#named-error-bags) 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](https://cdn-images-1.medium.com/max/843/1*M0RYqzUNe0gjTh5bMfqU7w.png)
_Laravel validation for multiple forms on the same page_

Thank you for reading!

_Follow me at_ [**_balajidharma.medium.com_**](https://balajidharma.medium.com/).

Add your feedback and comment If missed any other ways to display an error message for multiple forms.

* * *