0
0
Laravelframework~10 mins

Conditional validation in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to require the 'email' field only if 'subscribe' is true.

Laravel
$request->validate([
    'email' => '[1]:subscribe,true'
]);
Drag options to blanks, or click blank then click option'
Arequired_if
Brequired
Cnullable
Dsometimes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'required' makes the field always required.
Using 'nullable' allows empty values even when required.
Using 'sometimes' does not enforce requirement based on another field.
2fill in blank
medium

Complete the code to require 'password' only when 'change_password' is present in the request.

Laravel
$request->validate([
    'password' => '[1]'
]);
Drag options to blanks, or click blank then click option'
Arequired_if:change_password,true
Bsometimes
Crequired_without:change_password
Drequired_with:change_password
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'required_if' expects a specific value, not just presence.
Using 'required_without' triggers when the other field is missing.
Using 'sometimes' does not enforce requirement.
3fill in blank
hard

Fix the error in the validation rule to require 'phone' only if 'contact_method' equals 'phone'.

Laravel
$request->validate([
    'phone' => 'required_if:[1]'
]);
Drag options to blanks, or click blank then click option'
Acontact_method=phone
Bcontact_method,phone
Ccontact_method==phone
Dcontact_method
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' or '==' instead of a comma.
Omitting the value after the field name.
Using only the field name without value.
4fill in blank
hard

Fill both blanks to require 'address' only when 'country' is 'US' and 'state' is present.

Laravel
$request->validate([
    'address' => '[1]:country,US|[2]:state'
]);
Drag options to blanks, or click blank then click option'
Arequired_if
Brequired_without
Crequired_with
Dnullable
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'required_without' instead of 'required_with'.
Mixing up the order of rules.
Using 'nullable' which allows empty values.
5fill in blank
hard

Fill all three blanks to require 'discount_code' only if 'user_type' is 'member', 'purchase_amount' is greater than 100, and 'promo' is present.

Laravel
$request->validate([
    'discount_code' => '[1]:user_type,member|[2]:purchase_amount,100|[3]:promo'
]);
Drag options to blanks, or click blank then click option'
Arequired_if
Bgt
Crequired_with
Drequired
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'required' alone makes the field always required.
Using 'required_without' instead of 'required_with'.
Using '=' instead of ',' in rule parameters.