0
0
Laravelframework~10 mins

Form request classes 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 form request class using the artisan command.

Laravel
php artisan make:[1] StoreUserRequest
Drag options to blanks, or click blank then click option'
Acontroller
Brequest
Cmodel
Dmigration
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'make:controller' instead of 'make:request'.
Using 'make:model' or 'make:migration' which create different classes.
2fill in blank
medium

Complete the method name to define validation rules in a form request class.

Laravel
public function [1](): array
{
    return [
        'name' => 'required|string|max:255',
        'email' => 'required|email|unique:users,email',
    ];
}
Drag options to blanks, or click blank then click option'
Arules
Bmessages
Cauthorize
Dvalidate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'authorize' instead of 'rules'.
Using 'validate' which is not a method in form request classes.
3fill in blank
hard

Fix the error in the authorize method to allow all users to make the request.

Laravel
public function authorize(): bool
{
    return [1];
}
Drag options to blanks, or click blank then click option'
Afalse
Bnull
C1
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 'false' which denies all requests.
Returning 'null' or '1' which are not boolean true.
4fill in blank
hard

Fill both blanks to customize the error message for the 'email' field in a form request class.

Laravel
public function [1](): array
{
    return [
        'email.[2]' => 'Please provide a valid email address.',
    ];
}
Drag options to blanks, or click blank then click option'
Amessages
Brules
Crequired
Dunique
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'rules' instead of 'messages' method.
Using 'unique' instead of 'required' for the error message key.
5fill in blank
hard

Fill all three blanks to create a form request class that authorizes all users, defines rules, and custom messages.

Laravel
class StoreProductRequest extends FormRequest
{
    public function [1](): bool
    {
        return [2];
    }

    public function [3](): array
    {
        return [
            'name' => 'required|string',
            'price' => 'required|numeric|min:0',
        ];
    }
}
Drag options to blanks, or click blank then click option'
Aauthorize
Btrue
Crules
Dmessages
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing 'rules' and 'authorize' method names.
Returning false in 'authorize' method.