Laravel 9 Form Validation

Shani SinghShani Singh
2 min read

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

Things we will talk about in this post.

Validation Topics

After Installing Laravel we will create a sample form to see our validation.

1 - Create Form

To create a form in resources/views folder create a view file form.blade.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 a validation Rule write inside the controller

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

To display the error message on Input Fields

@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 the controller

$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 a Request File For Validation

To Create a Request File we have to run a command

php artisan make:request FormDataRequest

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

<?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 a complete video tutorial on Youtube.

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

Thank You for Reading

Reach Out To me. Twitter Instagram YouTube

0
Subscribe to my newsletter

Read articles from Shani Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shani Singh
Shani Singh

Laravel | VueJs | PHP | Shopify | ReactJs | MySql