In this post, we will learn how to create custom validation rules in the laravel 12 application.
Laravel provides default validation rules such as email, required, unique, date, and more. If you need to create a custom validation rule in Laravel, I can guide you through the steps
In this example, we will create a custom validation rule called BirthYearRule. We will add an input text box for birth_year and validate that the user enters a year between 1980 and the current year using our custom validation. You Can Learn Laravel 12 Image Intervention Tutorial With Example
Step 1: Install Laravel 12
First of all, we need to get a fresh Laravel 12 version application using the command below because we are starting from scratch. So, open your terminal or command prompt and run the command below:
composer create-project laravel/laravel example-app
Step 2: Create Routes
In this step, we will create two routes: GET and POST for a custom validation rule example. So let's add it.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CustomValidationController;
Route::get('custom-validation', [CustomValidationController::class, 'create']);
Route::post('custom-validation', [CustomValidationController::class, 'store'])->name('custom.validation.post');
Top comments (0)