For some reason, only now I’ve found out about a feature that was introduced in Laravel 5.5 – artisan command that makes your validation rule, similar to Request classes. Let’s see it in action.
Let’s take an example of a form to fill in Summer Olympic Games events – so year and city:
Now, let’s create a validation rule that you can enter only the year of Olympic Games:
- Games started in 1896
- Year can’t be bigger than current year
- Number should be divided by 4
Let’s run a command:
php artisan make:rule OlympicYear
Laravel generates a file app/Rules/OlympicYear.php:
namespace App\Rules; use Illuminate\Contracts\Validation\Rule; class OlympicYear implements Rule { /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { // } /** * Get the validation error message. * * @return string */ public function message() { return 'The validation error message.'; } }
As I said, it’s similar to Requests classes for validation. We fill in the methods. passes() should return true/false depending on $value condition, which is this in our case:
public function passes($attribute, $value) { return $value >= 1896 && $value <= date('Y') && $value % 4 == 0; }
Next, we fill in the error message to be this:
public function message() { return ':attribute should be a year of Olympic Games'; }
Finally, how we use this class? In controller's store() method we have this code:
public function store(Request $request) { $this->validate($request, ['year' => new OlympicYear]); }
Pay attention to syntax, second parameter should be an array and then we create a new object from our Rule class.
That's it!
That’s good, but I got used to Requests
You can use custom validation rules in requests also
You can use it in Request by following :
1) while create condition in Rule file you have to return true or false as per condition ,like:
if( $value > 2){
return false;
}
2) After create Rule, you have to include this in Request Like:
use App\Rules\OlympicYear;
3) use rule like that in Request
public function rules()
{
return [
“field_name” => [‘required’, new OlympicYear()],
];
}
I think you got a code error in $this->validate($request, [‘year’ => new OddNumber]);
should be $this->validate($request, [‘year’ => new OlympicYear]);
Cheers
Thanks a lot Jorge! Indeed was testing with a few different classes before writing the article. Fixed now.
That’s good, but I got used to Requests. Thank you for sharing.
Hello,
I think php artisan make:rule is no more available in larval latest version
I can confirm in 5.7 make:rule does still exist
v 5.8 still doest exist
It’s still exist’s in 5.8
This is really the best answer that I found on the net. It is well explained and follows Laravel convention.
Thank you. Just keep writing tutorials.
You need to import App\Rules\ OlympicYear in your controller.
Thank you Povilas for this brief explanation of the make:rule command. So as I understand we we use the command for validation purposes ?
We can use also custom validation with Laravel existing validation rules?
like I have create new custom validation called “IsProductExits” and using that I can also use like this
‘product_name’ => [ ‘bail’, ‘required’, new IsProductExits( $request->brand_id, $request->id ), ‘regex:/[A-Za-z0-9]/’, ‘min:1’, ‘max:30’ ],