How to Validate Input Field to Contain Only Two Specific Values in Laravel?

I need to validate an input field to accept only one of two specific values. For example, the input field should only allow either “open” or “close”.

<select name="status">
  <option value="open">Open</option>
  <option value="close">Close</option>
</select>

How can I achieve this validation in Laravel?

To validate an input field to contain only two specific values in Laravel, you can utilize the in validation rule. Here’s how you can implement it in your Laravel controller or form request validation:

public function store(Request $request)
{
    $request->validate([
        'status' => 'required|in:open,close'
    ]);
}

OR

use Illuminate\Validation\Rule;
public function store(Request $request)
{
    $request->validate([
        'status' => ['required', Rule::in(['courier', 'rider'])]
    ]);
}

This so do, what you are looking for.