DEV Community

Cover image for HOW TO VALIDATE FORM IN LARAVEL 9?
shani singh
shani singh

Posted on

HOW TO VALIDATE FORM IN LARAVEL 9?

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 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


Enter fullscreen mode Exit fullscreen mode

2 - Create Validation

To create validation Rule write inside controller



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


Enter fullscreen mode Exit fullscreen mode

To Display the error message on Input Fields



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


Enter fullscreen mode Exit fullscreen mode

3 - Create Validation Using Validator Facade

To create validation using Validator Facade we can write in 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();
}


Enter fullscreen mode Exit fullscreen mode

4 - Create Request File For Validation

To Create a Request File we have to run a command



php artisan make:request FormDataRequest


Enter fullscreen mode Exit fullscreen mode

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



<?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.',
        ];
    }
}


Enter fullscreen mode Exit fullscreen mode

Here you will get complete video tutorial on Youtube.

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

Thank You for Reading

Reach Out To me.
Twitter
Instagram
YouTube

Top comments (1)

Collapse
 
hernangior profile image
Hernandez

excellent content, it was exactly what I was looking for to improve my project