Laravel 8 Form Request Validation

How to make Form Request Validation in Laravel 8


In this article, you will be learning how to do validation in laravel using form request validation in laravel 8 application.

So guys, you have already created your application and going to insert data where you will be using Form Request Validation. We will be using Student Data Example. 

Let's get started with inserting data using laravel 8 form request validation.


Step 1: Create the route

Route::get('students', [App\Http\Controllers\StudentController::class,'create']);
Route::post('students', [App\Http\Controllers\StudentController::class,'store']);

Step 2: Create a Request Class with the following command:

$ php artisan make:request StudentFormRequest

After successfully creating of Form Request Validation Class, lets setup the rules for it as below:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StudentFormRequest 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
     */
    public function rules()
    {
        $rules = [
            'fullname' => [
                'required',
                'string',
                'max:191',
            ],
            'email' => [
                'required',
                'email',
                'max:191',
                'unique:students,email',
            ],
            'phone' => [
                'required',
                'digits:10',
            ],
            'course' => [
                'required',
                'string',
                'max:191',
            ],
        ];

        return $rules;
    }

}


Step 3: Go to your controller, example: StudentController.php and create the two function for create form and store form as below:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\StudentFormRequest;
use App\Models\Student;
use Illuminate\Http\Request;

class StudentController extends Controller
{
    public function create()
    {
        return view('student.create');
    }

    public function store(StudentFormRequest $request)
    {
        $data = $request->validated();
        Student::create($data);
        return redirect('/students')->with('message','Student Added Successfully');
    }
}


Step 4: Create a file named create.blade.php in folder named student in the following path: resources/views/student/create.blade.php and paste the give code:

@extends('layouts.app')

@section('content')

    <div class="py-4">
        <div class="container">
            <div class="row">
                <div class="col-md-12">

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

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

                    <div class="card">
                        <div class="card-header">
                            <h4>Add Student with Form Request Validation in Laravel 8
                                <a href="{{ url('students') }}" class="btn btn-danger float-end">BACK</a>
                            </h4>
                        </div>
                        <div class="card-body">
                            <form action="{{ url('students') }}" method="POST">
                                @csrf

                                <div class="mb-3">
                                    <label>Full Name</label>
                                    <input type="text" name="fullname" class="form-control">
                                </div>
                                <div class="mb-3">
                                    <label>Email ID</label>
                                    <input type="text" name="email" class="form-control">
                                </div>
                                <div class="mb-3">
                                    <label>Phone</label>
                                    <input type="text" name="phone" class="form-control">
                                </div>
                                <div class="mb-3">
                                    <label>Course</label>
                                    <input type="text" name="course" class="form-control">
                                </div>
                                <div class="mb-3">
                                    <button type="submit" class="btn btn-primary">Save Student</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

@endsection


That's it. Now, you are done with Form Request Validation in Laravel 8.

Thanks for reading.