Laravel 11 Form Validation Example

To validate form in Laravel 11; Simply create a form with input fields and apply validation rules using the built-in validation rules to validate form fields in Laravel 11 applications, and it will validate your form with the given rules.

In this guide, we will create a form with some fields and validate form fields using validation rules on the controller and also display a validation message when the form is validated without using any external library or package.

How to Validate Form in Laravel 11 Applications?

Steps to validate form in laravel 11 applications:

Step 1 – Setup New Laravel Application

To install and setup Laravel 11 in the system, you need to do it by typing the following command in CMD or on a terminal window:

composer create-project --prefer-dist laravel/laravel myApp

Step 2 – Create Form Controller & Validation Method

Now to create a controller class, type php artisan make:controller FromValidateController command on cmd or terminal window:

cd myApp
php artisan make:controller FromValidateController

Navigate to app/http/controllers folder and open the FromValidateController.php file, in this controller, you will have to create methods to display the form and validate the form data or fields on the controller, Something a bit like this:

    public function index()
    {
        return view('form');
    }

    public function validateForm(Request $request)
    {

        $validatedData = $request->validate([
          'name' => 'required',
          'email' => 'required|email',
          'phone_no' => 'required|max:11',
        ]);

        //implement code to insert form data after validation

        return redirect('form')->with('status', 'Form Data Has Been validated');

    }

Step 3 – Create Form Routes

Navigate to the routes folder and open the web.php file, in this web.php file you need to create a routes to display the form and send the form data to the controller for validation, something like this:

use App\Http\Controllers\FromValidateController;

Route::get('form', [FromValidateController::class, 'index']);
Route::post('validate-form', [FromValidateController::class, 'validateForm']);

Step 4 – Create Form Blade View

To create a form blade view file, simply navigate to the resources/views folder and create form.blade.php, which is used to send the form data to the controller for validation and display the message after validation:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 11 Form Validation Example - Tutsmake.com</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

</head>
<body>

<div class="container mt-4 text-center">

  @if(session('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
  @endif

    <div class="card-header text-center font-weight-bold">
      <h2>Laravel 11 Form Validation - Tutsmake.com</h2>
    </div>
      <form name="formValidationExample" id="formValidationExample" method="post" action="{{url('validate-form')}}">
       @csrf

        <div class="mb-3">
          <label for="exampleInputEmail1">Name</label>
          <input type="text" id="name" name="name" class="@error('name') is-invalid @enderror form-control">
          @error('name')
              <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
          @enderror
        </div>

        <div class="mb-3">
          <label for="exampleInputEmail1">Email</label>
          <input type="email" id="email" name="email" class="@error('email') is-invalid @enderror form-control">
          @error('email')
              <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
          @enderror
        </div>

        <div class="mb-3">
          <label for="exampleInputEmail1">Phone No</label>
          <input type="number" id="phone_no" name="phone_no" class="@error('phone_no') is-invalid @enderror form-control">
          @error('phone_no')
              <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
          @enderror
        </div>

        <button type="submit" class="btn btn-primary">Submit</button>
      </form>
    </div>
</body>
</html>

Step 5 – Run Development Server

To start development server for laravel application, simply run php artisan serve command on cmd or terminal window:

php artisan serve

And open your browser and hit the following URL on it:

http://127.0.0.1:8000/form

Conclusion

We hope you have learned how to validate your form data on controller with the help of this guide, if you still have any doubt then you can ask by commenting or mail us on [email protected].

Recommended Guides

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *