In this tutorial you will learn about the Laravel 7 Form Validation Request Class Example and its application with practical example.
In this Laravel 7 Form Validation Request Class Example tutorial, I’ll show you how to validate form data using request class in laravel apps. In laravel request validation class is used to validate form data on server in laravel apps. This step by step tutorial we will be creating request validation class and validate form data on server side in laravel apps.
Laravel 7 Form Validation Request Class Example
Step 1: Create Request Class
1 |
cd blog |
Next, run the following command to create validate request class in laravel:
1 |
php artisan make:request ValidateUser |
Now, go to app/Http/Requests and open ValidateUser.php class. Then update the following code into ValidateUser.php Class file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class ValidateUser 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 2: Add Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 |
Route::get('user/create', 'HomeController@create'); Route::post('user/create', 'HomeController@store'); |
Step 3: Create Controller
Now, lets create a controller named UserController using command given below –
1 |
php artisan make:controller UserController |
Now, navigate to app/Http/Controllers and open UserController.php file. Then update the following code into your UserController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; use App\Http\Requests\ValidateUser; class UserController 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 View
In this step, we will create a blade view file named createUser.blade.php. Go to resources/views/ folder and create blade view file here. Then put the following code into your createUser.blade.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<!DOCTYPE html> <html> <head> <title>Laravel 7 form validation example</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> |