About Us Blog Contact Us Request a quote

Laravel 12 form request validation

Install Laravel 12 App


composer create-project laravel/laravel example-app

Create route

routes/web.php like as below code


<?php

use App\Http\Controllers\ContactController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::resource('contacts', ContactController::class);

Create Form request file


php artisan make:request ContactFormRequest

App/Http/Requests/ContactFormRequest.php like as below code


<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array|string>
     */
    public function rules(): array
    {
        return [
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:contacts,email',
            'contact_no' => 'required|numeric|unique:contacts,phone',
            'address' => 'required|text',
        ];
    }
}

Create controller


php artisan make:controller ContactController --resource

App/controller/ContactController.php like as below code


<?php

namespace App\Http\Controllers;

use App\Http\Requests\ContactFormRequest;
use Illuminate\Http\Request;

class ContactController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
       
        return view('contact.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(ContactFormRequest $request)
    {
       
        return ''Contact successfully added.';
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
       //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
      //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
      //
    }
}

Create blade file

resources/contact/index.blade.php like as below code


<!DOCTYPE html>
<html lang="en">

<head>
<title>Laravel 12 FORM Request Validation-laravelexample.com<
/title>
<meta charset="utf-8"> <meta name="viewport" 
content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist
/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js
/bootstrap.bundle.min.js"></script>
</head>

<body>

<div class="container mt-3">
<div class="card">
<div class="card-header">
<h2>Laravel 12 FORM Request Validation </h2>
</div>
<div class="card-body">
               
@if (session()->has('success-message'))
<div class="alert alert-success" role="alert">
{{ session()->get('success-message') }}</div>
@endif
@if (session()->has('delete-message'))
<div class="alert alert-danger" role="alert" 
id="success-message">
{{ session()->get('delete-message') }}</div>
@endif

@if ($errors->any())
<div class="alert alert-danger mt-2">
<h6>Error Occurred! Please fix the following errors:</h6>
<ul>
 @foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

<form action="{{ route('contacts.store') }}" method="post">
@csrf
<div class="mb-3 mt-3">
<label for="name" class="form-label">Name:</label>
<input type="text" class="form-control" id="name"
placeholder="Enter name"
name="name">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email:</label>
<input type="email" class="form-control" id="email" 
placeholder="Enter email"
name="email">
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone:</label>
<input type="number" class="form-control" id="phone" 
placeholder="Enter phone"
name="phone">
</div>
<div class="mb-3">
<label for="address" class="form-label">Address:</label>
<textarea type="email" class="form-control" id="email" 
placeholder="Enter address"
name="address"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<div class="card-footer text-end"><a 
href="https://www.laravelexample.com/"
target="_blank">www.laravelexample.com</a></div>
</div>
</div> 

</html>

Run Laravel App:

Now Run your laravel 12 app after run these below commands


php artisan serve

Go to your web browser, hit this URL http://localhost:8000/contacts and see your laravel app output:









Laravel 12 form request validation

Laravel 12 form request validation

Recent Posts