Challenge - 5 Problems
Laravel Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when validation fails in Laravel's validate method?
Consider this Laravel controller method snippet:
What happens if the user submits an invalid email or age less than 18?
public function store(Request $request) {
$validated = $request->validate([
'email' => 'required|email',
'age' => 'required|integer|min:18'
]);
return 'Success';
}What happens if the user submits an invalid email or age less than 18?
Attempts:
2 left
💡 Hint
Think about Laravel's default behavior when validation fails in web requests.
✗ Incorrect
Laravel's validate method automatically redirects back to the previous page with error messages and old input if validation fails. It does not continue execution or throw a generic exception.
📝 Syntax
intermediate2:00remaining
Which validate method call is syntactically correct?
You want to validate a request to require a 'name' field as a string and an optional 'phone' field as numeric. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Order of rules matters for readability; 'required' usually comes first.
✗ Incorrect
In Laravel validation rules, 'required' should come before 'string'. Also, 'nullable' is the correct keyword for optional fields, not 'optional'. So option A is correct.
❓ state_output
advanced2:00remaining
What is the value of $validated after validation?
Given this code:
If the request data is {"title": "My Post", "published": "true", "extra": "ignore"}, what does $validated contain?
public function update(Request $request) {
$validated = $request->validate([
'title' => 'required|string',
'published' => 'boolean'
]);
return $validated;
}If the request data is {"title": "My Post", "published": "true", "extra": "ignore"}, what does $validated contain?
Attempts:
2 left
💡 Hint
Validated data only includes fields defined in rules and casts boolean strings.
✗ Incorrect
The validate method returns only the validated fields. The 'published' field is cast to boolean true from string 'true'. Extra fields are excluded.
🔧 Debug
advanced2:00remaining
Why does this validation code cause a runtime error?
Look at this code snippet:
What error occurs if the user submits no 'age' field, and why?
public function store(Request $request) {
$validated = $request->validate([
'email' => 'required|email',
'age' => 'integer|min:18'
]);
if ($validated['age'] >= 18) {
return 'Adult';
}
return 'Minor';
}What error occurs if the user submits no 'age' field, and why?
Attempts:
2 left
💡 Hint
Check if 'age' is required or optional in the rules and how $validated behaves.
✗ Incorrect
'age' is not required, so if missing, it won't be in $validated. Accessing $validated['age'] causes an undefined index error.
🧠 Conceptual
expert3:00remaining
Which option correctly validates nested array input with Laravel's validate method?
You expect a request with a 'user' object containing 'name' (string) and 'contacts' array of emails. Which validation rules correctly validate this structure?
Attempts:
2 left
💡 Hint
Remember to require the parent array before validating nested fields.
✗ Incorrect
Option D correctly requires 'user' as an array, then validates 'user.name' as string (not required), 'user.contacts' as array, and each contact as email. It properly orders required rules and array checks.