Challenge - 5 Problems
Conditional Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when conditional validation fails?
Consider this Laravel validation rule:
What will happen if
['email' => 'required_if:contact_method,email|email']
What will happen if
contact_method is set to phone and email is empty?Attempts:
2 left
💡 Hint
Think about when the 'required_if' rule applies.
✗ Incorrect
The 'required_if:contact_method,email' rule means the email field is only required if contact_method equals 'email'. If contact_method is 'phone', email is not required, so validation passes even if email is empty.
📝 Syntax
intermediate2:00remaining
Which validation rule correctly requires a field only when another field is present?
You want to require the
state field only if the country field is present in the request. Which validation rule achieves this?Attempts:
2 left
💡 Hint
Look for the rule that triggers requirement when another field exists.
✗ Incorrect
'required_with:country' means state is required only if country is present. 'required_if' needs a value to compare, 'required_unless' requires state unless country matches a value, and 'required_without' requires state if country is missing.
🔧 Debug
advanced2:00remaining
Why does this conditional validation not fail?
Given this Laravel validation rule:
and the input:
Why does validation pass even though the phone field is empty?
'phone' => 'required_if:contact_method,phone_number'
and the input:
['contact_method' => 'phone', 'phone' => '']
Why does validation pass even though the phone field is empty?
Attempts:
2 left
💡 Hint
Check the exact value used in the required_if condition and the input value.
✗ Incorrect
The rule requires phone only if contact_method equals 'phone_number'. The input has contact_method as 'phone', so the condition is false and phone is not required. Validation passes because phone is empty but not required. The issue is the misunderstanding of the condition value.
❓ state_output
advanced2:00remaining
What is the validation error message for this conditional rule?
Using this validation rule:
If the input is:
What error message will Laravel generate for password_confirmation?
'password_confirmation' => 'required_with:password|same:password'
If the input is:
['password' => 'secret', 'password_confirmation' => '']
What error message will Laravel generate for password_confirmation?
Attempts:
2 left
💡 Hint
Think about when required_with triggers and what same rule checks.
✗ Incorrect
The 'required_with:password' rule makes password_confirmation required if password is present. Since password_confirmation is empty, validation fails with 'The password confirmation field is required.' The 'same:password' rule is not checked because required_with already failed.
🧠 Conceptual
expert3:00remaining
How to conditionally validate a field based on multiple other fields?
You want to require the
zipcode field only if country is 'US' or state is present. Which validation rule correctly implements this?Attempts:
2 left
💡 Hint
Think about combining multiple rules in an array for OR conditions.
✗ Incorrect
Laravel does not support multiple conditions in a single required_if rule. To require zipcode only if country is 'US' OR state is present, combine 'required_if:country,US' and 'required_with:state' in an array. Option C correctly uses an array of rules to enforce either condition.