DEV Community

Cover image for How to Create Custom Validation Rules in Laravel 12
Msh Sayket
Msh Sayket

Posted on

How to Create Custom Validation Rules in Laravel 12

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
Enter fullscreen mode Exit fullscreen mode

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');


Enter fullscreen mode Exit fullscreen mode

Read More

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)