0
0
Laravelframework~20 mins

Conditional validation in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when conditional validation fails?
Consider this Laravel validation rule:
['email' => 'required_if:contact_method,email|email']

What will happen if contact_method is set to phone and email is empty?
AValidation passes but email format is still checked and causes an error.
BValidation fails because email is always required regardless of contact_method.
CValidation fails with an error that contact_method must be email.
DValidation passes because the email field is not required when contact_method is phone.
Attempts:
2 left
💡 Hint
Think about when the 'required_if' rule applies.
📝 Syntax
intermediate
2: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?
A'state' => 'required_if:country'
B'state' => 'required_with:country'
C'state' => 'required_unless:country'
D'state' => 'required_without:country'
Attempts:
2 left
💡 Hint
Look for the rule that triggers requirement when another field exists.
🔧 Debug
advanced
2:00remaining
Why does this conditional validation not fail?
Given this Laravel validation rule:
'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?
ABecause the required_if compares contact_method to 'phone_number', but the input has 'phone', so the condition is never true.
BBecause the required_if rule syntax is incorrect and causes a syntax error.
CBecause the phone field is always required regardless of contact_method value.
DBecause the phone field is required if contact_method equals 'phone_number', but contact_method is 'phone', so validation fails.
Attempts:
2 left
💡 Hint
Check the exact value used in the required_if condition and the input value.
state_output
advanced
2:00remaining
What is the validation error message for this conditional rule?
Using this validation rule:
'password_confirmation' => 'required_with:password|same:password'

If the input is:
['password' => 'secret', 'password_confirmation' => '']

What error message will Laravel generate for password_confirmation?
AThe password confirmation field is required.
BThe password confirmation and password must match.
CNo error, validation passes because password_confirmation is optional.
DThe password field is required.
Attempts:
2 left
💡 Hint
Think about when required_with triggers and what same rule checks.
🧠 Conceptual
expert
3: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?
A'zipcode' => 'required_if:country,US|required_with:state'
B'zipcode' => 'required_if:country,US,state'
C'zipcode' => ['required_if:country,US', 'required_with:state']
D'zipcode' => 'required_if:country,US|required_if:state,present'
Attempts:
2 left
💡 Hint
Think about combining multiple rules in an array for OR conditions.