0
0
Laravelframework~20 mins

Validate method on request 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 validation fails in Laravel's validate method?
Consider this Laravel controller method snippet:
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?
AThe method returns 'Success' regardless of validation errors.
BLaravel automatically redirects back with error messages and old input.
CThe method throws a generic Exception stopping execution.
DThe method ignores validation and saves data anyway.
Attempts:
2 left
💡 Hint
Think about Laravel's default behavior when validation fails in web requests.
📝 Syntax
intermediate
2: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?
A$request->validate(['name' => 'required|string', 'phone' => 'nullable|numeric']);
B$request->validate(['name' => 'required|string', 'phone' => 'numeric|optional']);
C$request->validate(['name' => 'string|required', 'phone' => 'optional|numeric']);
D$request->validate(['name' => 'string|required', 'phone' => 'numeric|nullable']);
Attempts:
2 left
💡 Hint
Order of rules matters for readability; 'required' usually comes first.
state_output
advanced
2:00remaining
What is the value of $validated after validation?
Given this code:
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?
A{"title": "My Post"}
B{"title": "My Post", "published": "true", "extra": "ignore"}
C{"title": "My Post", "published": true}
D{"title": "My Post", "published": false}
Attempts:
2 left
💡 Hint
Validated data only includes fields defined in rules and casts boolean strings.
🔧 Debug
advanced
2:00remaining
Why does this validation code cause a runtime error?
Look at this code snippet:
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?
AUndefined index error because 'age' is not required and may be missing in $validated.
BNo error; 'age' defaults to 0 automatically.
CValidation error because 'age' is required but missing.
DSyntax error due to missing semicolon.
Attempts:
2 left
💡 Hint
Check if 'age' is required or optional in the rules and how $validated behaves.
🧠 Conceptual
expert
3: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?
A['user.name' => 'required|string', 'user.contacts.*' => 'required|email']
B['user.name' => 'string|required', 'user.contacts' => 'required|array', 'user.contacts.*' => 'email|required']
C['user' => 'array', 'user.name' => 'required|string', 'user.contacts.*' => 'email|required']
D['user' => 'required|array', 'user.name' => 'string', 'user.contacts' => 'array', 'user.contacts.*' => 'email']
Attempts:
2 left
💡 Hint
Remember to require the parent array before validating nested fields.