Bird
0
0

How can you validate that a field status only accepts the values 'pending', 'approved', or 'rejected' in Laravel?

hard📝 Conceptual Q9 of 15
Laravel - Request and Response
How can you validate that a field status only accepts the values 'pending', 'approved', or 'rejected' in Laravel?
A'status' => 'required|in:pending,approved,rejected'
B'status' => 'required|string|exists:statuses'
C'status' => 'required|enum:pending,approved,rejected'
D'status' => 'required|regex:/pending|approved|rejected/'
Step-by-Step Solution
Solution:
  1. Step 1: Identify rule to restrict values to a list

    The 'in' rule restricts a field to specific allowed values.
  2. Step 2: Check other options for correctness

    'exists' checks database, 'enum' is not a Laravel rule, 'regex' is possible but complex.
  3. Final Answer:

    'status' => 'required|in:pending,approved,rejected' -> Option A
  4. Quick Check:

    Use 'in' to limit allowed values [OK]
Quick Trick: Use 'in' rule for specific allowed values [OK]
Common Mistakes:
  • Using 'exists' for value list
  • Assuming 'enum' is a Laravel rule
  • Overcomplicating with regex

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes