Search
Laravel 10 multiple form validation on the same page-WebJourney

Laravel 10 multiple form validation on the same page-WebJourney

How to handle multiple form validation on the same page with laravel ? Today we will learn multiple form validation on the same page with laravel 10, if you want you can use other versions of laravel framework.

 

Laravel has message bag to validate the multiple forms on the same page. We have to use the messge bag to validate and display the error messages above the form.

 

Validate form data using validateWithBag method in Laravel 8, 9, 10

 

ValidateFormController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class ValidateFormController extends Controller
{
   public function validate_form_one(Request $request)
    {
        Validator::make($request->all(), [
            'name' => 'required|max:191',
        ])->validateWithBag('user');

        // Code after validation
    }


    public function validate_form_two(Request $request)
    {
        Validator::make($request->all(), [
            'email' => 'required|email|max:191',
        ])->validateWithBag('user');

        // Code after validation
    }
    
}

 

 

 

Display errors using MessageBag instance from the $errors variable

 

resources\views\multi-form.blade.php

<div class="container mt-5">
    <div class="row">

        @if($errors->user->any())
            <div class="alert alert-danger">
                <ul>
                    @foreach($errors->user->all() as $error)
                        <li> {{ $error }} </li>
                    @endforeach
                </ul>
            </div>
        @endif

        <div class="col-md-6">
            <form action="{{route('form.one')}}" method="post">
                @csrf
                <h2>Form One</h2>
                <div class="mb-3">
                    <label>Name</label>
                    <input type="text" class="form-control" name="name">
                </div>
                <input type="submit" value="Submit" class="btn btn-primary">
            </form>
        </div>

        <div class="col-md-6">
            <form action="{{route('form.two')}}" method="post">
                @csrf
                <h2>Form Two</h2>
                <div class="mb-3">
                    <label>Email</label>
                    <input type="email" class="form-control" name="email">
                </div>
                <input type="submit" value="Submit" class="btn btn-primary">
            </form>
        </div>

    </div>
</div>

 

Hope this will help you a lot. Feel free to ask your question if any issue facing.

 

 

 

If you like what you are reading, please think about buying us a coffee as a token of appreciation.

Buy Me A Coffee

We appreciate your support and are committed to providing you useful and informative content.

We are thankful for your ongoing support                                                                                                                                                         

 

 

 

 

You May Read Bellow Articles:

 

 

Laravel 9 live search data in a table using ajax. 

 

How to send SMS in laravel 9 using Twilio SMS API-Webjourney

 

Laravel 9 pdf invoice generate and download with barryvdh dompdf

 

How to create multi language website by laravel 9

 

Laravel image crud with radio, checkbox, email, date, text, number, color, select box

 

🎯 Interactive Skill Assessment
Laravel Challenge: How Well Do You Know the PHP Framework Devs Love?

Test what you just learned with 31 interactive multiple-choice questions and instant explanations.

Start Free Quiz
  • Updated: Aug 1, 2023
  • 4568

Tags

Share This:

You May Also Like Below Articles

Handpicked related tutorials and step-by-step developer guides to continue learning.

Comments - 1

Nazmul Hoque

Nice sir. Thanks a lot for such a good blog.

Apr 9, 2023

WebJourney

Thank you too.

May 7, 2023