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.

Read Also:  Laravel Calculate Age from Date of Birth in MySQL

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","[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:  Bind Array List to Checkboxes using Angular Example

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  country state city drop down list using JavaScript
  2. Read Also:  call to undefined function str_slug()
  3. Read Also:  Dropdown List using Angular Example
  4. Read Also:  jQuery Delete Multiple Rows using checkbox PHP with MySQL
  5. Read Also:  Fix 504 Gateway Timeout Nginx Error
  6. Read Also:  PHP Laravel Sessions Example Tutorial
  7. Read Also:  How to Open URL in New Tab using Yii Framework
  8. Read Also:  Vue js Remove Last Comma from String
  9. Read Also:  Dropdown List using Angular Example
  10. Read Also:  Vue js Remove Last Comma from String
CLOSEX