HOW TO VALIDATE FORM IN LARAVEL 9?
Tags: laravel, webdev, beginners, programming
Published: 2022-06-16 19:55:07

In this post, I am going to cover A to Z about Laravel Validation.

Things we will talk about in this post.

![Validation Topics](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tj7rhjqz3rm6prqtfm6v.png)

After [Installing Laravel](https://dev.to/shanisingh03/how-to-install-laravel-9-25c4) we will create a sample form to see our validation.

## 1 - Create Form

To create form in `resources/views` folder create a view file `form.blade.php`.

```php
@extends('app')

@section('content')

    <!-- Container (Contact Section) -->
    <div id="contact" class="container">
        <h3 class="text-center">Create User</h3>
        <p class="text-center"><em>Register Here</em></p>

        <div class="row">
            
            <div class="col-md-12">
                <div class="row">
                    <div class="col-sm-6 form-group">
                        <input class="form-control" id="name" name="name" placeholder="Full Name" type="text" required>
                    </div>
                    <div class="col-sm-6 form-group">
                        <input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-6 form-group">
                        <input class="form-control" id="gender" name="gender" placeholder="Gender" type="text" required>
                    </div>
                    <div class="col-sm-6 form-group">
                        <input class="form-control" id="password" name="password" placeholder="Password" type="password" required>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-12 form-group">
                        <button class="btn pull-right" type="submit">Send</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
```

## 2 - Create Validation

To create validation Rule write inside controller

```php
$request->validate([
            'name' => 'required',
            'email' => 'required',
            'gender' => 'required',
            'password' => 'required',
        ]);
```

To Display the error message on Input Fields

```php
@error('name')
   <span class="text-danger">{{$message}}</span>
@enderror
```

## 3 - Create Validation Using Validator Facade

To create validation using Validator Facade we can write in controller

```php
$validate = Validator::make($request->all(), [
            'name' => 'required|min:5',
            'email' => 'required',
            'gender' => 'required',
            'password' => 'required',
        ],[
            'name.required' => 'Name is must.',
            'name.min' => 'Name must have 5 char.',
        ]);
if($validate->fails()){
  return back()->withErrors($validate->errors())->withInput();
}
```

## 4 - Create Request File For Validation

To Create a Request File we have to run a command 

```php
php artisan make:request FormDataRequest
```

This will create a file inside `App\Http\Requests` folder.
The file will be like.

```php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class FormDataRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'name' => 'required|min:5',
            'email' => 'required',
            'gender' => 'required',
            'password' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'name.required' => 'Name is Must',
            'name.min' => 'Name Must be 5 Chr.',
        ];
    }
}
```

Here you will get complete video tutorial on Youtube.

{% youtube YjAT4IVz0dg %}

If you face any issues while implementing, please comment your query.

Thank You for Reading

Reach Out To me.
[Twitter](https://twitter.com/shanisingh03)
[Instagram](https://www.instagram.com/shanisingh03/)
[YouTube](https://www.youtube.com/c/TechToolIndia)