How to create custom validation rules with Laravel?

0 votes
As we know that laravel allow many pre-defined service so i wanted to know how to create custom validation rules with laravel?

Thank you!!
Mar 24 in Laravel by kartik
• 10,760 points
26 views

1 answer to this question.

0 votes

Hii @kartik,

Follow are the steps to create custome validation rules in Laravel:

  • Run this "php artisan make:rule OlympicYear"
  • After that command it generates a file app/Rules/OlympicYear.php
  • We can write rule in the passes() in OlympicYear.php generated file. It will return true or false depending on condition, which is this in our case

  • public function passes($attribute, $value)
    {
    return $value >= 1896 && $value <= date('Y') && $value % 4 == 0;
    }
  • Next, we can update error message to be this:

  • public function message()
    {
    return ':attribute should be a year of Olympic Games';
    }
  • Finally, we use this class in controller's store() method we have this code:
  • public function store(Request $request)
    {
    $this->validate($request, ['year' => new OlympicYear]);
    }

Thank you!!

answered Mar 24 by Niroj
• 21,680 points

Related Questions In Laravel

0 votes
1 answer

How to pass CSRF token with ajax request in Laravel?

Hey, In between head, tag put <meta name="csrf-token" ...READ MORE

answered Mar 24 in Laravel by Dey
96 views
0 votes
1 answer

How to solve expected response code 220 but got code “”, with message “” in Laravel?

Hii @kartik, if you are using Swift Mailer: ...READ MORE

answered Mar 31 in Laravel by Niroj
• 21,680 points
680 views
0 votes
1 answer

How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?

Hello, To create both of the created_at and updated_at columns: $t->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); $t->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update ...READ MORE

answered Apr 2 in Laravel by Niroj
• 21,680 points
279 views
0 votes
1 answer

How to connect to mysql with laravel?

Hello, Laravel makes it very easy to manage ...READ MORE

answered Apr 9 in Laravel by Niroj
• 21,680 points
41 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 21,680 points
58 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 21,680 points
63 views
0 votes
1 answer

What are named routes in Laravel?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 21,680 points
128 views
0 votes
1 answer

How to create database migration in Laravel?

Hii, Migrations are like version control for your database, ...READ MORE

answered Mar 23 in Laravel by Niroj
• 21,680 points
50 views
0 votes
1 answer

How to make a new page with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 21,680 points
70 views
0 votes
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 21,680 points
394 views