Laravel 5.5 custom validation rules example (ok)
https://www.itsolutionstuff.com/post/laravel-55-custom-validation-rules-exampleexample.html
Một ví dụ đã hoàn thành
C:\xampp\htdocs\reset\routes\web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
use App\Http\Controllers\HomeController;
use App\Http\Controllers\FormController;
Route::get('/', function () {
dd('Welcome to simple user route file.');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get("form",[FormController::class,'index'])->name('form.index');;
Route::post("form",[FormController::class,'store'])->name('form.store');;C:\xampp\htdocs\reset\app\Http\Controllers\FormController.php
C:\xampp\htdocs\reset\app\Http\Controllers\CheckOddEvenRule.php
C:\xampp\htdocs\reset\app\Rules\CheckOddEvenRule.php
C:\xampp\htdocs\reset\resources\views\myForm.blade.php

Laravel 5.5 custom validation rules example
By Hardik Savani September 10, 2017 Category : PHP LaravelPlayUnmuteLoaded: 1.15%Fullscreen
As we know very well about, few days ago laravel 5.5 released and they introduce several feature. They also change method of create custom validation rules. So in this post i explain you how to create custom validation rules in laravel 5.5 application.
Validation is a primary requirement of every project, without validation we can not release successful application or website. Because if you haven't implemented validation then you get wrong information from use input. Laravel provide there are several default validation rules like required, max, min, array, unique, digits, in, boolean, before, after etc. We can simple use those validation rules in laravel 5.5 application. But if you require to create your own custom validation like given value should be in uppercase or lowercase etc as we require.
Here, i will simple represent how to make custom validator rules in laravel 5.5 application and it is very simple, so you have to just follow bellow step, make sure you can create only for laravel 5.5, if you want to create on laravel 5.3, or 5.2 then you can follow this tutorial Laravel 5 - Create Custom Validation Rules.
So let's follow bellow step, i make example from scratch. In this example i take number text field and you can just enter even number, If you enter odd number then you got validation error.

Step 1: Create Custom Validation Rule
In first step, we will create custom validation rules by run following command. Bellow command through you can create Validation rules file and we can write a logic on that file. So let's run bellow command.
Ok, after successfully run above command, you will found new "rules" folder in app directory. In rules folder you will also found CheckOddEvenRule.php file. We have to simple write logic there, if you enter even number then return true otherwise don't do anything.
So, you should have file like as bellow:
app/Rules/CheckOddEvenRule.php
Step 2: Add Routes
Here, we need to add two routes for create form get method and another for post method. so open your routes/web.php file and add following route.
routes/web.php
Read Also: How to create custom helper in Laravel?
Step 3: Create FormController
Here, now we should create new controller as FormController. So run bellow command and create new controller. bellow controller for create controller.
After bellow command you will find new file in this path app/Http/Controllers/FormController.php.
In this controller will create seven methods by default as bellow methods:
1)index()
2)store()
So, let's copy bellow code and put on FormController.php file.
app/Http/Controllers/FormController.php
Step 4: Create View File
Finally at the end of this example, we have to create myForm.blade.php file, in this file we will create form and display validation error so let's create myForm.blade.php file and put bellow code:
resources/views/myForm.blade.php
Now we are ready to run our custom validation rules example so run bellow command for quick run:
Now you can open bellow URL on your browser:
Read Also: Custom User Log Activity in Laravel App Example
I hope it can help you...
Watch Online Video:
Last updated
Was this helpful?