0
0
Laravelframework~10 mins

Custom validation rules in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a custom validation rule class in Laravel.

Laravel
php artisan make:[1] UniqueEmail
Drag options to blanks, or click blank then click option'
Amake:rule
Bvalidation
Cmake:validation
Drule
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'make:validation' instead of 'make:rule'.
Using just 'rule' without 'make:'.
2fill in blank
medium

Complete the method signature to define the validation logic inside the custom rule class.

Laravel
public function [1]($attribute, $value) { }
Drag options to blanks, or click blank then click option'
Acheck
Bvalidate
Cpasses
Dhandle
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'validate' or 'check' instead of 'passes'.
Missing the required parameters in the method.
3fill in blank
hard

Fix the error in the validation logic to fail validation when the value is not 'admin'.

Laravel
if ($value !== '[1]') {
    $fail('The :attribute must be admin.');
}
Drag options to blanks, or click blank then click option'
Auser
Badmin
Cguest
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using other role names like 'user' or 'guest' which do not match the requirement.
Leaving the string empty or misspelled.
4fill in blank
hard

Fill both blanks to register the custom rule in a form request's rules method.

Laravel
public function rules() {
    return [
        'email' => ['required', new [1]()],
        'username' => ['required', '[2]'],
    ];
}
Drag options to blanks, or click blank then click option'
AUniqueEmail
Bstring
Cemail
Dunique
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'email' or 'unique' as the custom rule class name.
Using the custom rule class name as a string rule.
5fill in blank
hard

Fill all three blanks to create a custom validation rule that checks if a number is even.

Laravel
public function passes($attribute, $value) {
    return $value [1] 2 [2] 0;
}

public function message() {
    return '[3]';
}
Drag options to blanks, or click blank then click option'
A%
B===
CThe :attribute must be an even number.
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '===' which is strict equality and may cause issues with type.
Incorrect message text or missing message method.