0
0
Laravelframework~20 mins

Form request classes in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Request Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Laravel form request class fails validation?
Consider a Laravel form request class used in a controller method. What is the default behavior when the validation rules defined in the form request fail?
AThe request is silently accepted without any validation.
BThe controller method runs but with empty input data.
CThe request is redirected back to the previous page with validation errors and old input data.
DThe application throws a fatal error and stops execution.
Attempts:
2 left
💡 Hint
Think about user experience when submitting invalid form data.
📝 Syntax
intermediate
1:30remaining
Which method must be defined in a Laravel form request class to specify validation rules?
In a Laravel form request class, which method is used to define the validation rules for the incoming request data?
Amessages()
Bvalidate()
Cauthorize()
Drules()
Attempts:
2 left
💡 Hint
This method returns an array of rules.
state_output
advanced
2:00remaining
What is the value of $validated after calling validated() on a form request?
Given this Laravel form request class with rules:
public function rules() {
    return [
        'name' => 'required|string',
        'age' => 'nullable|integer|min:18'
    ];
}

And the incoming request data:
{"name": "Alice", "age": "20"}

What will $validated = $request->validated(); contain?
A{"name" => "Alice", "age" => 20}
B{"name" => "Alice", "age" => "20"}
C{"name" => "Alice"}
DAn empty array
Attempts:
2 left
💡 Hint
Consider how Laravel casts validated input types.
🔧 Debug
advanced
2:00remaining
Why does this Laravel form request always fail authorization?
Given this form request class snippet:
public function authorize()
{
    return false;
}

What is the effect of this code when used in a controller method?
AAll requests using this form request will be denied with a 403 Forbidden response.
BThe request will always pass authorization and proceed.
CThe request will throw a validation error.
DThe request will redirect back with an error message.
Attempts:
2 left
💡 Hint
Think about what returning false in authorize() means.
🧠 Conceptual
expert
2:30remaining
How does Laravel handle validation error messages in form request classes?
In Laravel form request classes, how can you customize the validation error messages for specific rules?
ABy setting a public property $messages with the custom messages array.
BBy defining a messages() method that returns an array mapping rules to custom messages.
CBy overriding the rules() method to include messages inline.
DBy editing the config/validation.php file only.
Attempts:
2 left
💡 Hint
Look for a method dedicated to messages in the form request class.