Define Custom Laravel Validation Rules Example

Today, We want to share with you Define Custom Laravel Validation Rules Example.In this post we will show you How to Define Custom Laravel 5.7 Rules Validation Method Examp, hear for Laravel validator addmethod custom error message we will give you demo and example for implement.In this post, we will learn about PHP Laravel validate add Custom validation rule with an example.

Define Custom Laravel Validation Rules Example

There are the Following The simple About Define Custom Laravel Validation Rules Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 5.7 custom validation rules, so the Laravel Custom Validation Rules for this example is following below.

Create Laravel Custom Validation Rule

php artisan make:rule FrmCheckEvenRule

app/Rules/FrmCheckEvenRule.php

<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class FrmCheckEvenRule 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 :Laravel All the attribute must be even value.';
    }
}

Add Routes

routes/web.php

Route::get("form","SignUpController@index");
Route::post("form","SignUpController@store");

Create SignUpController

php artisan make:controller SignUpController

app/Http/Controllers/SignUpController.php

validate([
                'name' => 'required',
                'number' => [
                    'required', 
                    new FrmCheckEvenRule()
                ]
            ]);
       dd("You can Now All the proceed now...");
    }
}

Laravel Create View File

resources/views/home.blade.php




    Laravel 5.7 custom validation rules example - pakainfo.com
    
    
    
    


  

Laravel 5.7 custom validation rules - Pakainfo.com

{{ csrf_field() }}
@if ($errors->has('name')) {{ $errors->first('name') }} @endif
@if ($errors->has('number')) {{ $errors->first('number') }} @endif
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Define Custom Laravel Validation Rules Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment