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.

Read Also:  Hello World Example using Laravel MVC

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","[email protected]");
Route::post("form","[email protected]");

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



    <title>Laravel 5.7 custom validation rules example - pakainfo.com</title>
    
    
    
    


  <div class="container">
    <h1>Laravel 5.7 custom validation rules - Pakainfo.com</h1>
    <form method="POST" action="{{ url('form') }}">
      {{ csrf_field() }}


      <div class="form-group">
        <label>Name:</label>
        
        @if ($errors->has('name'))
          <span class="text-danger">{{ $errors->first('name') }}</span>
        @endif
      </div>

      <div class="form-group">
        <label>Number:</label>
        
        @if ($errors->has('number'))
          <span class="text-danger">{{ $errors->first('number') }}</span>
        @endif
      </div>
      <div class="form-group">
        <button class="btn btn-success btn-submit">Submit</button>
      </div>
    </form>
  </div>


Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  react open new tab on button click

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.