0
0
Laravelframework~5 mins

Custom validation rules in Laravel - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom validation rule in Laravel?
A custom validation rule in Laravel is a user-defined rule that lets you check data in ways not covered by Laravel's built-in validators. It helps you create specific checks for your app's needs.
Click to reveal answer
beginner
How do you create a custom validation rule class in Laravel?
Use the Artisan command: <code>php artisan make:rule RuleName</code>. This creates a class where you define the <code>passes()</code> method to check the value and <code>message()</code> to return an error message.
Click to reveal answer
beginner
What does the passes() method do in a Laravel custom rule?
The passes() method checks if the given value meets your custom condition. It returns true if valid, or false if invalid.
Click to reveal answer
intermediate
How do you apply a custom validation rule to a form request in Laravel?
In your form request's <code>rules()</code> method, add the custom rule class instance to the field's rules array, like <code>'field' => ['required', new RuleName()]</code>.
Click to reveal answer
intermediate
Can custom validation rules accept parameters? How?
Yes. You can pass parameters via the rule class constructor when creating the rule instance. Then use those parameters inside <code>passes()</code> to customize validation.
Click to reveal answer
Which Artisan command creates a custom validation rule class?
Aphp artisan make:rule RuleName
Bphp artisan make:validation RuleName
Cphp artisan create:rule RuleName
Dphp artisan generate:rule RuleName
What should the passes() method return if the value is valid?
Anull
Btrue
Cfalse
Dan error message
Where do you add a custom validation rule in Laravel?
AIn the routes file
BIn the controller's constructor
CIn the form request's <code>rules()</code> method
DIn the model's <code>boot()</code> method
How do you pass parameters to a custom validation rule?
AVia the rule class constructor
BBy setting global variables
CUsing environment variables
DBy modifying the <code>message()</code> method
What does the message() method in a custom rule do?
AChecks if the value is valid
BDefines the validation logic
CSaves the validation result
DReturns the error message when validation fails
Explain how to create and use a custom validation rule in Laravel.
Think about the steps from creating the rule to applying it in validation.
You got /4 concepts.
    Describe how you can customize a Laravel validation rule with parameters.
    Focus on how data flows into the rule class.
    You got /3 concepts.