20

Is there a built in validator in Laravel 5 that checks if value is in array of my whitelisted values sort of speak.. Something like:

$rules = [
    'field_name' => "required|in_array('yes', 'no', 'maybe')",
];
0

3 Answers 3

53

There's in

$rules = [
    'field_name' => "required|in:yes,no,maybe",
];
Sign up to request clarification or add additional context in comments.

1 Comment

What about when the values you are checking against contain commas?
8

Laravel 5.7

use Illuminate\Validation\Rule;

Validator::make($data, [
    'field_name' => [
        'required',
        Rule::in(['yes', 'no', 'maybe']),
    ],
]);

Comments

0

In addition to this one, I needed a validation if filed_name starts with one of the items from the array, so you can use starts_with in the same manner

$rules = [
    'field_name' => "required|starts_with:foo,bar",
];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.