Laravel 6 Custom Validation Rule Example

Laravel 6 , Laravel , Laravel 7

Nicesnippets

2334

29-01-2020


Hi Guys

Today, I will show you can how to custom validation rule in laravel 7/6, we will explain laravel 7/6 custom validation rule example custom validation rule is very usefull and interesting concept of laravel 7/6 because custom validation throught we can re-use validation and use it easily by just name as like laravel core

In this example, i am going to show you how to create custom validation rules in laravel 7/6 you can create very simple way and use too. you can create custom validation rules by few following step and you can see preview of example that give for add custom validation rule. In this example you are creating is_even_string

custom validation rule.

here following steps of custom validation rule in laravel 7/6

Step 1 : Create Route

In first step we will create two route for example, one for get method will help to generate view and second for post method that help for from submit

following path:/routes/web.php

Route::get('customValiRule', 'CustomValRuleController@customValiRule');

Route::post('customValiRulePost', 'CustomValRuleController@customValiRulePost');

Step 2 : Create Controller

here this step now we should create new controller as CustomValRuleController,So run bellow command for generate new controller

php artisan make:controller CustomValRuleController

now this step, this controller will manage custom validation bellow content in controller file.following fille path

following path:/app/Http/Controllers/CustomValRuleController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CustomValRuleController extends Controller

{

public function customValiRule()

{

return view('customRuleVali');

}

public function customValiRulePost(Request $request)

{

$this->validate($request, [

'title' => 'required|is_even_string',

]);

print_r('done');

}

}

Step 3: Declare Validation

In this step we will declare and write code of our custom validation. So first open app/Providers/AppServiceProvider.php file and put following code:

following path:/app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Illuminate\Support\Facades\Schema;

use Validator;

class AppServiceProvider extends ServiceProvider

{

/**

* Register any application services.

*

* @return void

*/

public function boot()

{

Validator::extend('is_even_string', function($attribute, $value, $parameters, $validator) {

if(!empty($value) && (strlen($value) % 2) == 0){

return false;

}

return true;

});

}

/**

* Bootstrap any application services.

*

* @return void

*/

public function register()

{

//

}

}

After,we have to give error message for new created custom validation rules, so register custom message open resoueces/lang/en/validation.php and put one line as i did bellow:

following path:/resoueces/lang/en/validation.php

return [

'is_even_string' => "The :attribute must be odd string lenght.",

'accepted' => 'The :attribute must be accepted.'

.....

]

Step 4: Create View

In last step, you have to create one view for demo. so just copy bellow code and put on following file.

following path:/resources/views/customRuleVali.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 6 Custom Validation Rule Example - nicesnippets.com </title>

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.css">

</head>

<body class="bg-dark">

<div class="container">

<div class="row">

<div class="col-md-8 offset-md-2">

<div class="card mt-5">

<div class="card-header">

<h5>Laravel 6 Custom Validation Rule Example - nicesnippets.com </h5>

</div>

<div class="card-body">

<form action="{{ URL::to('customValiRulePost') }}" method="post">

@csrf

@if (count($errors) > 0)

<div class="alert alert-danger">

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif

<div class="row">

<div class="col-md-12">

<div class="form-group">

<label>Title<span class="text-danger">*</span></label>

<input type="text" name="title" class="form-control" placeholder="Enter title">

</div>

</div>

</div>

<div class="row">

<div class="col-md-12">

<button class="btn btn-success">Submit</button>

</div>

</div>

</form>

</div>

</div>

</div>

</div>

</div>

</body>

</html>

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/customValiRule

It will help you...


Recommended Posts