Bird
0
0

You want to validate a request so that the phone field is optional but if present must be numeric and exactly 10 digits. Which validation rule is correct?

hard📝 Conceptual Q8 of 15
Laravel - Request and Response
You want to validate a request so that the phone field is optional but if present must be numeric and exactly 10 digits. Which validation rule is correct?
A'phone' => 'sometimes|string|min:10'
B'phone' => 'required|numeric|digits:10'
C'phone' => 'nullable|numeric|digits:10'
D'phone' => 'optional|digits_between:10,10'
Step-by-Step Solution
Solution:
  1. Step 1: Identify optional field rule

    'nullable' allows the field to be missing or null but validates if present.
  2. Step 2: Check numeric and length rules

    'numeric' ensures only numbers, 'digits:10' requires exactly 10 digits.
  3. Final Answer:

    'phone' => 'nullable|numeric|digits:10' -> Option C
  4. Quick Check:

    Optional numeric 10 digits = 'nullable|numeric|digits:10' [OK]
Quick Trick: Use 'nullable' for optional fields [OK]
Common Mistakes:
  • Using 'required' instead of 'nullable'
  • Confusing 'sometimes' with 'optional'
  • Using 'digits_between' incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes