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, 2020 in Laravel by kartik
• 37,520 points
1,164 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, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

What is database migration and how to create database migration in Laravel?

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

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,246 views
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, 2020 in Laravel by Dey
39,562 views
0 votes
2 answers

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

This problem can generally occur when you ...READ MORE

answered Dec 16, 2020 in Laravel by Gitika
• 65,890 points
42,116 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, 2020 in Laravel by Niroj
• 82,880 points
11,959 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

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

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
22,845 views
0 votes
1 answer

What is redirection in Laravel?

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

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,922 views
0 votes
1 answer

How to install Laravel via composer?

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

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,860 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

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

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
44,124 views
0 votes
1 answer

How to give custom field name in laravel form validation error message?

Hello @kartik, You can specify custom error message ...READ MORE

answered Sep 30, 2020 in Laravel by Niroj
• 82,880 points
4,794 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, 2020 in Laravel by Niroj
• 82,880 points
12,906 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP