0
0
Laravelframework~10 mins

Custom validation rules in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom validation rules
Start Form Submission
Trigger Validation
Check Built-in Rules
Invoke Custom Rule Class
Run Custom Logic
Return Pass/Fail
If Fail -> Show Error
If Pass -> Continue Processing
When a form is submitted, Laravel runs built-in and custom validation rules. Custom rules run your own logic and return pass or fail.
Execution Sample
Laravel
use Illuminate\Contracts\Validation\Rule;

class Uppercase implements Rule {
  public function passes($attribute, $value) {
    return strtoupper($value) === $value;
  }
  public function message() {
    return 'The :attribute must be uppercase.';
  }
}
This custom rule checks if a string is all uppercase and returns an error message if not.
Execution Table
StepActionInput Valuepasses() ResultMessage ReturnedValidation Outcome
1Validate 'name' with Uppercase ruleJOHNtrueN/APass
2Validate 'name' with Uppercase ruleJohnfalseThe name must be uppercase.Fail
3Validate 'name' with Uppercase ruleDOEtrueN/APass
4Validate 'name' with Uppercase ruledoefalseThe name must be uppercase.Fail
5Validation completeN/AN/AN/AStop
💡 Validation stops after all inputs are checked and errors collected or passed.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
input_valueN/AJOHNJohnDOEdoeN/A
passes_resultN/AtruefalsetruefalseN/A
validation_outcomeN/APassFailPassFailN/A
Key Moments - 3 Insights
Why does the validation fail when the input is 'John' but pass when it is 'JOHN'?
The passes() method checks if the input is exactly uppercase. 'John' has lowercase letters, so passes() returns false (see execution_table step 2).
What happens if the message() method is missing in the custom rule?
Laravel will throw an error or show a default message. The message() method provides the error text when validation fails (see execution_table steps 2 and 4).
Does the custom rule run before or after built-in rules?
All rules run during validation. Custom rules run alongside built-in ones when listed. Here, the custom rule runs when validating the 'name' field (concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the passes() result at step 4?
Afalse
Btrue
CN/A
Derror
💡 Hint
Check the passes() Result column at step 4 in the execution_table.
At which step does the validation outcome become 'Fail' for the first time?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the Validation Outcome column in the execution_table.
If the input 'name' was 'DOE', what would the validation outcome be according to the execution_table?
AError
BFail
CPass
DUnknown
💡 Hint
Refer to step 3 in the execution_table for input 'DOE'.
Concept Snapshot
Custom validation rules in Laravel let you write your own checks.
Create a class implementing Rule with passes() and message() methods.
passes() returns true or false based on your logic.
message() returns the error text if validation fails.
Use the rule in your validator like built-in rules.
This helps enforce rules Laravel doesn't have by default.
Full Transcript
When you submit a form in Laravel, the framework checks your data using validation rules. Sometimes, you need special checks that Laravel doesn't provide. You can create a custom validation rule by making a class that implements the Rule interface. This class has a passes() method where you write your check, and a message() method that returns the error message if the check fails. For example, a rule can check if a string is all uppercase. When Laravel runs validation, it calls your passes() method with the input. If it returns false, Laravel shows your message. This process happens for each input you validate. This way, you can make your forms smarter and catch mistakes users might make.