In Laravel Form Request Validation Example Guide, I will show you easy way to validate form data using request->validate() method with its rules in Laravel 11 applications.
Laravel 11 Form Validation Example
You can validate your form and display messages in laravel 11 application using the following steps:
Step 1: Download Laravel Application
To install Laravel 11 application in your system, you can do it with the help of this command:
composer create-project laravel/laravel blog
Step 2: Create Form
Create a myform.blade.php file inside the resources/views folder, and create a form in the myform.blade.php file so that the user can request it for validation and display a message:
<!DOCTYPE html>
<html>
<head>
<title>Laravel 11 Form Validation Example - ItcodStuff.com</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="card mt-5">
<h3 class="card-header p-3"><i class="fa fa-star"></i> Laravel 11 Form Validation Example - itcodstuff.com</h3>
<div class="card-body">
@session('success')
<div class="alert alert-success" role="alert">
{{ $value }}
</div>
@endsession
<form method="POST" action="{{ url('validate') }}" name="myFormValidaiton">
{{ csrf_field() }}
<div class="mb-3">
<label class="form-label" for="inputName">Name:</label>
<input
type="text"
name="name"
id="inputName"
class="form-control @error('name') is-invalid @enderror"
placeholder="Name">
<!-- Way 2: Display Error Message -->
@error('name')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<label class="form-label" for="inputEmail">Email:</label>
<input
type="text"
name="email"
id="inputEmail"
class="form-control @error('email') is-invalid @enderror"
placeholder="Email">
@error('email')
<span class="text-danger">{{ $message }}</span>
@endif
</div>
<div class="mb-3">
<button class="btn btn-success btn-submit"><i class="fa fa-save"></i> Submit</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Step 3: Add Validation Rules in Method
Run the following command to create a controller file, after that add two methods into it to show form and validate form data using request->validate() rules;
php artisan make:controller FormValidationController
Now create two methods in app/http/controllers/FormValidationController.php
file to show form and validate form data using request->validate():
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
class FormValidationController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function formView(): View
{
return view('myform');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function validateData(Request $request): RedirectResponse
{
$validatedData = $request->validate([
'name' => 'required',
'email' => 'required|email'
]);
return back()->with('success', 'Form Data has been validated successfully.');
}
}
Step 4: Create Routes
Create two routes in the routes/web.php
file, which are used to display the form and send form data to the controller method for validation:
use App\Http\Controllers\FormValidationController;
Route::get('myform', [FormValidationController::class, 'formView']);
Route::post('validate', [FormValidationController::class, 'validateData']);
Step 5: Run Application Server
Type php artisan serve
command on cmd to run this application serve:
php artisan serve
Next open browser and type the following url into it:
http://127.0.0.1:8000/myform
Conclusion
Using this five step guide you can easily validate form data with build-in validation rules with request->validate in Laravel 11 application version.
You can check out more validations rules at https://laravel.com/docs/11.x/validation.