Conditional validation in Laravel means applying validation rules only when certain conditions are met. It helps to validate data dynamically based on other input values.
Click to reveal answer
beginner
How do you apply a validation rule only if another field has a specific value?
You can use the required_if rule. For example, 'field2' => 'required_if:field1,value' means field2 is required only if field1 equals value.
Click to reveal answer
intermediate
What Laravel method allows you to add validation rules conditionally in a fluent way?
The sometimes method lets you add rules only when a condition is true. For example, $validator->sometimes('field', 'rule', fn() => condition).
Click to reveal answer
intermediate
Explain the difference between required_if and sometimes in Laravel validation.
required_if is a rule that makes a field required based on another field's value. sometimes is a method to apply rules only when a condition is true, giving more control over when validation runs.
Click to reveal answer
beginner
How can you validate a field only if another field is present in the request?
Use the required_with rule. For example, 'field2' => 'required_with:field1' means field2 is required only if field1 is present in the input.
Click to reveal answer
Which Laravel validation rule makes a field required only if another field equals a specific value?
Anullable
Bsometimes
Crequired_with
Drequired_if
✗ Incorrect
required_if makes a field required only when another field has a given value.
What does the sometimes method do in Laravel validation?
AApplies rules only when a condition is true
BAlways applies validation rules
CSkips validation completely
DMakes a field optional
✗ Incorrect
sometimes applies validation rules only if a given condition is true.
Which rule requires a field only if another field is present in the input?
Arequired_with
Brequired_if
Crequired_without
Dsometimes
✗ Incorrect
required_with requires a field only if another specified field is present.
How do you add conditional validation rules using a closure in Laravel?
AUsing <code>required_if</code> with a closure
BUsing <code>sometimes</code> with a closure
CUsing <code>nullable</code> with a closure
DUsing <code>required_with</code> with a closure
✗ Incorrect
The sometimes method accepts a closure to conditionally apply rules.
What happens if you use required_if:status,active on a field?
AField is required if status is not 'active'
BField is always required
CField is required only if status is 'active'
DField is optional
✗ Incorrect
The field becomes required only when the status field equals 'active'.
Describe how you would validate a form field only when another field has a specific value in Laravel.
Think about rules that depend on other fields' values.
You got /3 concepts.
Explain the difference between the sometimes method and the required_if rule in Laravel validation.
One is a method for adding rules conditionally, the other is a specific rule.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of conditional validation in Laravel?
easy
A. To apply validation rules only when certain conditions are met
B. To validate all fields regardless of user input
C. To disable validation completely
D. To validate fields only on the server side
Solution
Step 1: Understand conditional validation concept
Conditional validation means rules run only if specific conditions are true.
Step 2: Compare options with concept
Only To apply validation rules only when certain conditions are met describes applying rules based on conditions, matching the concept.
Final Answer:
To apply validation rules only when certain conditions are met -> Option A
But it always fails even when status is inactive. What is the likely problem?
medium
A. The 'min:18' rule is applied even when 'age' is not required, causing failure
B. The 'required_if' rule is case sensitive and should be 'Required_If'
C. The 'required_if' rule applies only when status is 'active', so failure means another issue
D. The pipe character '|' is missing between rules
Solution
Step 1: Analyze rule application order
All rules run if field is present; 'min:18' runs even if 'age' is empty but present.
Step 2: Understand why validation fails when status is 'inactive'
Since 'age' is not required, but if present and empty, 'min:18' fails because empty is invalid.
Final Answer:
The 'min:18' rule is applied even when 'age' is not required, causing failure -> Option A
Quick Check:
min rule runs if field present, causing failure [OK]
Hint: Use 'nullable' to allow empty when not required [OK]
Common Mistakes:
Thinking 'required_if' is case sensitive
Ignoring that min rule runs on empty values
Assuming pipe character is missing
5. You want to validate a form where the password field is required only if the user_type is 'admin'. Also, if password is present, it must be at least 8 characters. Which validation rule set correctly implements this?
hard
A. 'password' => ['nullable', 'min:8']
B. 'password' => ['required_if:user_type,admin', 'string', 'min:8']
C. 'password' => ['required_if:user_type,admin', 'min:8']
D. 'password' => ['required_if:user_type,admin', 'nullable', 'min:8']
Solution
Step 1: Identify requirement for password field
Password is required only if user_type is 'admin', so 'required_if:user_type,admin' is needed.
Step 2: Ensure password length validation applies only if present
Adding 'string' and 'min:8' ensures password is string and minimum length 8 when present.
Step 3: Check options for correct combination
'password' => ['required_if:user_type,admin', 'string', 'min:8'] correctly combines required_if, string, and min:8 without nullable, so password is required conditionally and validated properly.
Final Answer:
'password' => ['required_if:user_type,admin', 'string', 'min:8'] -> Option B