Laravel Form Validation Request Class Example

By Hardik Savani April 16, 2024 Category : Laravel

In this tute, we will discuss laravel form validation request class example. i would like to share with you laravel request validation class example. We will look at example of handle request validation in laravel. I’m going to show you about validation request rules class with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 app.

In this tutorial, i will show you how smart way we can handle form request validation in laravel. we will create new request class for adding validation for our form in laravel. it's best way for developing to create new class for request and handle it.

So, here in this tutorial, you have to follow few step to create form validation with request class in laravel. here we will create StoreUser request class and declare rules here.

Let's follow bellow step and will get layout as like bellow:

Preview:

Step 1: Create Routes

Here we are learning simple and easy example of validation in laravel 7 so just add following both route in your web.php file.

routes/web.php

Route::get('user/create', 'HomeController@create');

Route::post('user/create', 'HomeController@store');

Step 2: Create Request Class

Here, we will create request class where we will define validation rules. so let's create "StoreUser" class using bellow command:

php artisan make:request StoreUser

now, we will update rules as bellow:

app/Http/Requests/StoreUser.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreUser 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()

{

return [

'name' => 'required',

'username' => 'required|min:8',

'email' => 'required|email|unique:users'

];

}

}

Step 3: Create Controller

Now we will add two controller method, one will just display blade file with get request, and another for post request with validation, we will use StoreUser class for validation, i write validation for that, so simply add both following method on it.

app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

use App\Http\Requests\StoreUser;

class HomeController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function create()

{

return view('createUser');

}

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function store(StoreUser $request)

{

$input = $request->all();

$user = User::create($input);

return back()->with('success', 'User created successfully.');

}

}

Step 4: Create Blade File

now here we will create createUser.blade.php file and here we will create bootstrap simple form with error validation message. So, let's create following file:

resources/views/createUser.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 7 form validation example - ItSolutionStuff.com</title>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">

<h1>Laravel 7 form validation example</h1>

@if(Session::has('success'))

<div class="alert alert-success">

{{ Session::get('success') }}

@php

Session::forget('success');

@endphp

</div>

@endif

<form method="POST" action="{{ url('user/create') }}">

{{ csrf_field() }}

<div class="form-group">

<label>Name:</label>

<input type="text" name="name" class="form-control" placeholder="Name">

@if ($errors->has('name'))

<span class="text-danger">{{ $errors->first('name') }}</span>

@endif

</div>

<div class="form-group">

<label>Password:</label>

<input type="password" name="password" class="form-control" placeholder="Password">

@if ($errors->has('password'))

<span class="text-danger">{{ $errors->first('password') }}</span>

@endif

</div>

<div class="form-group">

<strong>Email:</strong>

<input type="text" name="email" class="form-control" placeholder="Email">

@if ($errors->has('email'))

<span class="text-danger">{{ $errors->first('email') }}</span>

@endif

</div>

<div class="form-group">

<button class="btn btn-success btn-submit">Submit</button>

</div>

</form>

</div>

</body>

</html>

Now we can run and check full example.

I hope it can help you...

Shares