Bird
Raised Fist0
Laravelframework~20 mins

Validate method on request in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the validate method on a Laravel request do?
easy
A. It saves the user input directly to the database.
B. It checks if the user input meets specified rules and returns errors if not.
C. It formats the user input into JSON automatically.
D. It sends an email notification when input is received.

Solution

  1. Step 1: Understand the purpose of validate

    The validate method is designed to check user input against rules.
  2. Step 2: Recognize Laravel's automatic error handling

    If input does not meet rules, Laravel sends errors back automatically without saving or formatting data.
  3. Final Answer:

    It checks if the user input meets specified rules and returns errors if not. -> Option B
  4. Quick Check:

    Validation = Input check and error return [OK]
Hint: Validate means check input rules and return errors [OK]
Common Mistakes:
  • Thinking validate saves data
  • Confusing validate with formatting methods
  • Assuming validate sends emails
2. Which of the following is the correct syntax to validate a request with a required email field in Laravel?
easy
A. $request->validate(['email' => required, 'email']);
B. $request->validate('email' => 'required|email');
C. $request->validate(['email' => 'required|email']);
D. $request->validate(['email' => required|email]);

Solution

  1. Step 1: Check the array syntax for rules

    Rules must be passed as an associative array with field names as keys and rule strings as values.
  2. Step 2: Confirm correct string format for rules

    Rules are strings separated by pipe characters, e.g., 'required|email'.
  3. Final Answer:

    $request->validate(['email' => 'required|email']); -> Option C
  4. Quick Check:

    Array with string rules = correct syntax [OK]
Hint: Use array with field => 'rules' string [OK]
Common Mistakes:
  • Missing array brackets
  • Not quoting the rules string
  • Separating rules incorrectly
3. Given this code snippet, what happens if the user submits an empty 'name' field?
$validated = $request->validate([
    'name' => 'required|string',
    'age' => 'nullable|integer'
]);
medium
A. The validation fails and Laravel redirects back with an error for 'name'.
B. The validation passes and $validated contains 'name' as an empty string.
C. The validation fails but no error message is shown.
D. The validation passes and 'age' is set to zero automatically.

Solution

  1. Step 1: Understand the 'required' rule on 'name'

    The 'required' rule means the 'name' field must be present and not empty.
  2. Step 2: Analyze empty input for 'name'

    An empty 'name' fails the 'required' rule, so validation fails and Laravel redirects back with errors.
  3. Final Answer:

    The validation fails and Laravel redirects back with an error for 'name'. -> Option A
  4. Quick Check:

    Empty required field = validation fail [OK]
Hint: Required means field cannot be empty [OK]
Common Mistakes:
  • Assuming empty string passes 'required'
  • Thinking validation passes silently
  • Confusing 'nullable' with 'required'
4. Identify the error in this validation code:
return $request->validate([
    'email' => 'required|email',
    'password' => required|min:8
]);
medium
A. The validate method should not return anything.
B. The 'email' rule is missing for the 'email' field.
C. The array brackets are missing around the rules.
D. The 'required' rule for 'password' is not quoted as a string.

Solution

  1. Step 1: Check the syntax of rules for each field

    Rules must be strings inside quotes, e.g., 'required|min:8'.
  2. Step 2: Identify missing quotes in 'password' rules

    The 'password' rules are not quoted, causing a syntax error.
  3. Final Answer:

    The 'required' rule for 'password' is not quoted as a string. -> Option D
  4. Quick Check:

    Rules must be strings in quotes [OK]
Hint: Always quote validation rules as strings [OK]
Common Mistakes:
  • Forgetting quotes around rules
  • Misplacing array brackets
  • Thinking validate returns void always
5. You want to validate a request so that the 'username' is required, must be a string, and unique in the 'users' table, but only if the user is creating a new record (not updating). Which validation rule is correct?
hard
A. $request->validate(['username' => 'required|string|unique:users']);
B. $request->validate(['username' => 'required|string|unique']);
C. $request->validate(['username' => 'required|string|unique:users,username,' . $user->id]);
D. $request->validate(['username' => 'required|string|unique:users.username']);

Solution

  1. Step 1: Understand the unique rule for new records

    For new records, the unique rule is simply 'unique:table' or 'unique:table,column'.
  2. Step 2: Identify the rule that applies only for new records

    $request->validate(['username' => 'required|string|unique:users']); uses 'unique:users' which checks uniqueness in the users table without exceptions.
  3. Step 3: Eliminate incorrect options

    A omits the table name, C is for updates (ignores current user ID), D uses invalid syntax 'unique:users.username' (dot instead of comma).
  4. Final Answer:

    $request->validate(['username' => 'required|string|unique:users']); -> Option A
  5. Quick Check:

    Unique rule with table name only = new record check [OK]
Hint: Use 'unique:table' for new record uniqueness [OK]
Common Mistakes:
  • Using update syntax for new records
  • Omitting table name in unique rule
  • Confusing unique rule format
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard