0
0
Laravelframework~20 mins

Request validation basics 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 happens when a Laravel request fails validation?

Consider a Laravel controller method that uses $request->validate() with rules. What is the default behavior if the validation fails?

Laravel
public function store(Request $request) {
    $validated = $request->validate([
        'name' => 'required|string',
        'email' => 'required|email'
    ]);
    // ... store logic
}
ALaravel automatically redirects back with error messages and old input.
BThe controller method returns null and does nothing.
CLaravel throws a fatal error and stops the application.
DThe request continues and the controller stores the data anyway.
Attempts:
2 left
💡 Hint

Think about how Laravel helps users fix form errors by showing messages.

📝 Syntax
intermediate
2:00remaining
Which validation rule syntax is correct for requiring a numeric age over 18?

Choose the correct Laravel validation rule string to require an age field that must be numeric and at least 18.

A'age' => 'required|numeric|min:18'
B'age' => 'required|number|min:18'
C'age' => 'required|integer|min18'
D'age' => 'required|numeric|min18'
Attempts:
2 left
💡 Hint

Look for the correct Laravel rule names and syntax for minimum values.

state_output
advanced
2:00remaining
What is the value of $validated after validation passes?

Given this Laravel controller code, what does $validated contain after validation?

Laravel
public function update(Request $request) {
    $validated = $request->validate([
        'title' => 'required|string|max:255',
        'content' => 'nullable|string'
    ]);
    return $validated;
}
ANull if any optional fields are missing.
BThe original Request object with all input data.
CA JSON string of all request data.
DAn array with only the validated fields and their values from the request.
Attempts:
2 left
💡 Hint

Think about what $request->validate() returns after success.

🔧 Debug
advanced
2:00remaining
Why does this Laravel validation code cause a syntax error?

Identify the syntax error in this validation rules array:

Laravel
public function store(Request $request) {
    $request->validate([
        'email' => 'required|email',
        'password' => 'required|min:8|max:20',
        'age' => 'nullable|numeric|min:18'
    ]);
}
AThe 'nullable' rule cannot be combined with 'numeric'.
BThe 'min:18' rule is invalid for numeric fields.
CMissing comma after the 'password' rule line causes a syntax error.
DThe 'email' rule must be last in the array.
Attempts:
2 left
💡 Hint

Look carefully at the commas between array elements.

🧠 Conceptual
expert
2:00remaining
Which option best explains Laravel's Form Request validation advantage?

Laravel offers Form Request classes for validation. What is the main advantage of using Form Requests over inline $request->validate() calls?

AForm Requests disable validation errors and always accept input.
BForm Requests separate validation logic from controllers, improving code organization and reuse.
CForm Requests run validation on the client side before submitting forms.
DForm Requests automatically fix invalid input data without developer code.
Attempts:
2 left
💡 Hint

Think about code organization and maintainability.