<?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');;
<?php
namespace App\Http\Controllers;
use App\Rules\CheckOddEvenRule;
use Illuminate\Http\Request;
class FormController extends Controller {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
return view('myForm');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
$request->validate([
'name' => 'required',
'number' => ['required', new CheckOddEvenRule()],
]);
dd("You can proceed now...");
}
}
<?php
namespace App\Http\Controllers;
class CheckOddEvenRule extends Controller {
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct() {}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value) {
if ($value % 2 == 0) {
return true;
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message() {
return 'The :attribute must be even value.';
}
}
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class CheckOddEvenRule implements Rule {
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct() {
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value) {
if ($value % 2 == 0) {
return true;
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message() {
return 'The :attribute must be even value.';
}
}
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.
php artisan make:rule CheckOddEvenRule
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
<?phpnamespace App\Rules;use Illuminate\Contracts\Validation\Rule;class CheckOddEvenRule implements Rule{ /** * Create a new rule instance. * * @return void */ public function __construct() { } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { if($value%2 == 0){ return true; } } /** * Get the validation error message. * * @return string */ public function message() { return 'The :attribute must be even value.'; }}
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.
Here, now we should create new controller as FormController. So run bellow command and create new controller. bellow controller for create controller.
php artisan make:controller FormController
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
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\Rules\CheckOddEvenRule;class FormController extends Controller{ /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return view('myForm'); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function store() { $input = request()->validate([ 'name' => 'required', 'number' => [ 'required', new CheckOddEvenRule() ] ]); dd("You can proceed now..."); }}
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:
By Hardik Savani September 10, 2017 Category : PHP LaravelPlayUnmuteLoaded: 1.15%FullscreenAs 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.