Challenge - 5 Problems
Laravel Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this custom validation rule in Laravel?
Consider this Laravel custom validation rule class that checks if a string contains only vowels. What will be the validation result for the input 'aeiou'?
Laravel
namespace App\Rules; use Illuminate\Contracts\Validation\Rule; class OnlyVowels implements Rule { public function passes($attribute, $value) { return preg_match('/^[aeiou]+$/i', $value) === 1; } public function message() { return 'The :attribute must contain only vowels.'; } } // Usage in a controller validation: $request->validate([ 'word' => ['required', new OnlyVowels], ]);
Attempts:
2 left
💡 Hint
Look at the regex pattern and the preg_match flags.
✗ Incorrect
The regex '/^[aeiou]+$/i' matches strings that contain only vowels, case-insensitive. 'aeiou' matches this pattern, so the validation passes.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a Laravel custom validation rule class?
Identify the option that correctly implements a Laravel custom validation rule class named 'Uppercase' that checks if a string is all uppercase.
Attempts:
2 left
💡 Hint
Check method signatures and syntax correctness.
✗ Incorrect
Option A correctly implements the Rule interface with the required passes method signature and uses strict equality. Option A incorrectly extends Rule (should implement), C uses loose equality (==), and D misses a semicolon causing syntax error.
🔧 Debug
advanced2:00remaining
Why does this Laravel custom validation rule always fail?
Given this custom rule code, why does validation always fail regardless of input?
class NumericString implements Rule {
public function passes($attribute, $value) {
return is_numeric($value) && is_string($value);
}
public function message() {
return 'The :attribute must be a numeric string.';
}
}
Attempts:
2 left
💡 Hint
Think about what is_numeric and is_string check for.
✗ Incorrect
is_numeric returns true for numeric strings and numbers, but is_string returns true only for strings. If $value is a number type, is_string returns false, so the combined condition fails. This causes the rule to fail for numeric strings that are not strings.
❓ state_output
advanced2:00remaining
What is the validation error message output for this custom rule?
If a Laravel custom validation rule class returns this message method:
public function message() {
return 'The :attribute must be a palindrome.';
}
And the attribute name is 'username', what is the final error message shown to the user?
Attempts:
2 left
💡 Hint
Laravel replaces :attribute with the field name in error messages.
✗ Incorrect
Laravel automatically replaces the placeholder :attribute with the actual field name, so the message becomes 'The username must be a palindrome.'
🧠 Conceptual
expert2:00remaining
Which statement about Laravel custom validation rules is TRUE?
Select the correct statement about Laravel custom validation rules.
Attempts:
2 left
💡 Hint
Think about how Laravel allows custom rules to be created.
✗ Incorrect
Laravel allows custom validation rules to be created either by implementing the Rule interface or by creating invokable classes. They do not need to extend Validator. The message() method returns a string, not boolean. Custom rules can access other data if passed properly.