Challenge - 5 Problems
Laravel Custom Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a validation fails with a custom error message in Laravel?
Consider this Laravel validation snippet inside a controller method:
What error message will the user see if the email field is left empty?
request()->validate(["email" => "required|email"], ["email.required" => "Email is mandatory."])
What error message will the user see if the email field is left empty?
Attempts:
2 left
💡 Hint
Custom messages override default messages for specific rules.
✗ Incorrect
Laravel uses the custom message provided for the 'email.required' rule instead of the default one.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a custom error message for the 'min' rule on a 'password' field?
You want to show the message "Password must be at least 8 characters." when the password is too short. Which code snippet correctly sets this in Laravel validation?
Attempts:
2 left
💡 Hint
The format is 'field.rule' as the key.
✗ Incorrect
Laravel expects custom messages keys in the format 'field.rule'. Only option C follows this.
🔧 Debug
advanced2:00remaining
Why does this custom error message not show up?
Given this validation code:
If the username contains numbers, what happens and why might the custom message not appear?
request()->validate([ 'username' => 'required|alpha' ], [ 'username.alpha' => 'Username must only contain letters.' ]);
If the username contains numbers, what happens and why might the custom message not appear?
Attempts:
2 left
💡 Hint
Check the rule name spelling and message key format.
✗ Incorrect
The 'alpha' rule disallows numbers, and the custom message key matches the rule, so the custom message appears.
❓ state_output
advanced2:00remaining
What is the output of this validation with multiple custom messages?
Consider this validation snippet:
If the age field is submitted as 16, what error message will be shown?
request()->validate([ 'age' => 'required|integer|min:18' ], [ 'age.required' => 'Age is required.', 'age.min' => 'You must be at least 18 years old.' ]);
If the age field is submitted as 16, what error message will be shown?
Attempts:
2 left
💡 Hint
Only the rules that fail trigger messages.
✗ Incorrect
Since age is provided but less than 18, only the 'min' rule fails and its custom message shows.
🧠 Conceptual
expert2:00remaining
How does Laravel prioritize custom error messages when both attribute and rule messages exist?
You define these messages:
When the email field is empty, which message will Laravel show and why?
[ 'email.required' => 'Email is required.', 'required' => 'This field is required.' ]
When the email field is empty, which message will Laravel show and why?
Attempts:
2 left
💡 Hint
Laravel uses the most specific message available.
✗ Incorrect
Laravel prioritizes messages for specific fields and rules over general rule messages.