In this tutorial, you will learn how to validate form data using request class in laravel apps.
The request validation class validate form data on server in laravel apps. This tutorial guide you step by step to creating request validation class and validate form data on server side in laravel apps.
Laravel Request Class Form Validation Example
Follow the below steps and create request validation class and validate form data in laravel apps:
Step 1: Create Request Class
First of all, open your terminal and navigate to your laravel project directory using the below command:
cd blog
Next, run the following command to create validate request class in laravel:
php artisan make:request ValidateUser
Now, Navigate to app/Http/Requests and open ValidateUser.php class. Then update the following code into ValidateUser.php Class file:
<?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
In this step, Navigate routes/web.php file and add the following routes into web.php file:
Route::get('user/create', 'HomeController@create'); Route::post('user/create', 'HomeController@store');
Step 3: Create Controller
In this step, open your terminal and run the following command to create controller:
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:
<?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, create one blade view file name createUser.blade.php. So navigate resources/views/ folder and create blade view file here.
Then update the following code into your createUser.blade.php file:
<!DOCTYPE html>
<html>
<head>
<title>Laravel 7 form validation example - Tutsmake.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>
Conclusion
In this laravel request class validation example, you have learned how to validate form data on server side using request class in laravel apps.