0
0
Laravelframework~20 mins

Custom validation rules in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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],
]);
AThe validation fails because the regex is incorrect and matches consonants.
BThe validation fails because 'aeiou' contains uppercase letters.
CThe validation passes because 'aeiou' contains only vowels.
DThe validation passes only if the input is exactly 'aeiou'.
Attempts:
2 left
💡 Hint
Look at the regex pattern and the preg_match flags.
📝 Syntax
intermediate
2: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.
A
class Uppercase implements Rule {
    public function passes($attribute, $value) {
        return strtoupper($value) === $value;
    }
    public function message() {
        return 'The :attribute must be uppercase.';
    }
}
B
class Uppercase extends Rule {
    public function passes($value) {
        return strtoupper($value) === $value;
    }
    public function message() {
        return 'The :attribute must be uppercase.';
    }
}
C
class Uppercase implements Rule {
    public function passes($attribute, $value) {
        return strtoupper($value) == $value;
    }
    public function message() {
        return 'The :attribute must be uppercase.';
    }
}
D
class Uppercase implements Rule {
    public function passes($attribute, $value) {
        return strtoupper($value) === $value
    }
    public function message() {
        return 'The :attribute must be uppercase.';
    }
}
Attempts:
2 left
💡 Hint
Check method signatures and syntax correctness.
🔧 Debug
advanced
2: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.'; } }
ABecause the message method should return a boolean, not a string.
BBecause the class does not implement the Rule interface.
CBecause the passes method is missing the $attribute parameter.
DBecause is_numeric returns true for numbers but is_string returns false for numeric values, so the condition is never true.
Attempts:
2 left
💡 Hint
Think about what is_numeric and is_string check for.
state_output
advanced
2: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?
Ausername must be a palindrome.
BThe username must be a palindrome.
CThe :attribute must be a palindrome.
DThe username must be a palindrome
Attempts:
2 left
💡 Hint
Laravel replaces :attribute with the field name in error messages.
🧠 Conceptual
expert
2:00remaining
Which statement about Laravel custom validation rules is TRUE?
Select the correct statement about Laravel custom validation rules.
ACustom validation rules can be implemented as invokable classes or using the Rule interface.
BCustom validation rules must always extend the base Validator class to work.
CCustom validation rules cannot access other request data during validation.
DCustom validation rules must return a boolean from the message() method.
Attempts:
2 left
💡 Hint
Think about how Laravel allows custom rules to be created.