Discover how to make your app smarter by creating your own data rules that work everywhere automatically!
Why Custom validation rules in Laravel? - Purpose & Use Cases
Imagine you have a form where users enter data, and you need to check if the data meets special rules that built-in checks don't cover.
You try to write all these checks manually every time you get input.
Manually checking each input everywhere is tiring and easy to forget.
It leads to repeated code, mistakes, and inconsistent checks across your app.
Custom validation rules let you write your special checks once and reuse them everywhere.
Laravel runs these rules automatically when validating input, keeping your code clean and reliable.
$input = request()->all(); if(strlen($input['username']) < 5) { return 'Username too short'; }
$request->validate([ 'username' => ['required', new CustomUsernameRule()] ]);
You can easily enforce any unique or complex data rules consistently across your whole application.
For example, checking if a username contains only allowed characters or if a coupon code is valid before accepting it.
Manual checks are repetitive and error-prone.
Custom validation rules centralize and reuse your special checks.
This keeps your app reliable and your code clean.
Practice
Solution
Step 1: Understand Laravel validation rules
Laravel provides built-in rules but sometimes you need to check data in a way not covered by them.Step 2: Purpose of custom validation rules
Custom rules let you write your own logic to validate data exactly as you want.Final Answer:
To define your own logic for validating data beyond built-in rules -> Option AQuick Check:
Custom validation = custom logic [OK]
- Thinking custom rules change database structure
- Confusing validation with form generation
- Assuming custom rules create tables
Solution
Step 1: Recall custom rule methods
Laravel custom validation rules require two methods:passesandmessage.Step 2: Identify method for validation logic
Thepassesmethod contains the logic to decide if the data is valid or not.Final Answer:
passes -> Option DQuick Check:
passes() checks data validity [OK]
- Using message() to check data validity
- Assuming validate() or check() are required methods
- Confusing method names
$rule->passes('age', 17) return?class AdultRule implements \Illuminate\Contracts\Validation\Rule {
public function passes($attribute, $value) {
return $value >= 18;
}
public function message() {
return 'You must be an adult.';
}
}Solution
Step 1: Understand the passes method logic
The method returns true if the value is 18 or more, false otherwise.Step 2: Evaluate passes with value 17
Since 17 is less than 18, the method returns false.Final Answer:
false -> Option CQuick Check:
17 < 18 means passes() = false [OK]
- Assuming 17 passes as adult
- Thinking passes() throws error for numbers
- Confusing message() with passes()
class EvenNumberRule implements \Illuminate\Contracts\Validation\Rule {
public function passes($attribute, $value) {
return $value % 2 == 0
}
public function message() {
return 'The number must be even.';
}
}Solution
Step 1: Check syntax in passes method
The return statement lacks a semicolon at the end, causing a syntax error.Step 2: Verify other method signatures
message() correctly returns a string, passes() correctly has two parameters, and implementing the interface is enough.Final Answer:
Missing semicolon after return statement in passes method -> Option BQuick Check:
Missing semicolon = syntax error [OK]
- Thinking message() must return boolean
- Assuming passes() takes one parameter
- Believing class must extend base class
passes method?Solution
Step 1: Understand the requirement
The string must contain only vowels (a, e, i, o, u) in any case.Step 2: Analyze each option
return preg_match('/^[aeiouAEIOU]+$/', $value) === 1; uses regex to check if the string contains only vowels from start to end. return strlen($value) > 0 && ctype_alpha($value); checks only letters but not vowels. return strpos($value, 'aeiou') !== false; checks if 'aeiou' substring exists, which is incorrect. return $value == strtolower($value); checks if string is lowercase, unrelated to vowels.Final Answer:
return preg_match('/^[aeiouAEIOU]+$/', $value) === 1; -> Option AQuick Check:
Regex matching only vowels = correct passes() [OK]
- Using ctype_alpha without vowel check
- Checking substring instead of full string
- Confusing case check with vowel check
Ready to prove it?
Pass the test and this pattern moves unknown
