Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel

Validating Booleans, Enums, and In-Array Values in Laravel

When a Laravel endpoint receives data from a form or JSON request, validation rules decide which values it accepts. The rules check different kinds of values. boolean accepts a defined set of true or false forms. enum checks named cases in a PHP enum, while in checks a fixed list. in_array checks whether a value appears in another submitted field. A PHP enum defines named cases, such as pending and active statuses.

Laravel 13’s boolean rule accepts six specific values. The other rules check the enum cases or lists you provide. Choose a rule based on where its allowed values come from, so a value does not pass the wrong check just because it looks valid.

boolean accepts six specific values

Laravel’s boolean rule accepts only true, false, integer 1, integer 0, string "1", and string "0". It rejects strings such as "true", "false", "yes", and "on". The Laravel validation documentation describes the rule.

'enabled' => ['boolean'],

A common mistake is to assume that boolean accepts every way to say yes or no. Laravel’s separate accepted rule checks inputs that show agreement, including values such as "yes" and "on". Use boolean when the field should hold a boolean value. Use accepted when it should record agreement or acceptance.

To accept only native PHP booleans, use strict mode:

'enabled' => ['boolean:strict'],

Strict mode accepts only true and false. Laravel also provides a fluent equivalent:

'enabled' => [Rule::boolean(true)],

In both strict forms, integer and string representations such as 1 and "0" fail validation.

enum checks cases from a PHP enum

A PHP enum defines named cases for statuses, such as pending and active. Laravel’s Rule::enum checks whether a value matches a case in the specified enum:

use Illuminate\Validation\Rule;

'status' => [Rule::enum(ServerStatus::class)],

For basic request values such as strings or integers, Laravel resolves a backed enum, whose cases each have a string or integer value. The framework calls the enum’s tryFrom method to find a matching case. If no case matches, validation fails. Laravel checks an enum instance directly. It cannot match primitive input this way against a plain enum, which has no backed values. See Laravel’s enum validation rule.

You can narrow the permitted cases with only or exclude cases with except:

'status' => [
    Rule::enum(ServerStatus::class)
        ->only([ServerStatus::Pending, ServerStatus::Active]),
],

These methods take enum cases, not arbitrary labels or descriptions. Use Rule::enum when your data is represented by a PHP enum. Use Rule::in for a list of allowed strings or integers.

in checks a fixed list

The in rule checks whether a submitted value appears in a list you specify. Laravel recommends Rule::in when you define the allowed values in an array:

'zone' => [
    'required',
    Rule::in(['first-zone', 'second-zone']),
],

The validation rule contains the list, so it does not depend on another submitted field. Use in for a fixed set of choices.

When a request field contains an array, apply the allowed-value rule to each element with Laravel’s * wildcard:

'airports' => ['required', 'array'],
'airports.*' => ['required', Rule::in(['NYC', 'LIT'])],

Here, airports must be an array, and each element must pass the in rule. Applying in to the whole array does not check each element. Laravel explains the wildcard pattern in its validation rules.

in_array compares against another input field

The in_array rule checks whether a field’s value appears among the values submitted in another field. For example:

'selected_role' => ['required', 'in_array:available_roles.*'],

selected_role must appear in the submitted available_roles array. The * refers to the values inside that array. With Rule::in([...]), the rule contains a fixed list; with in_array, the allowed values come from the request.

Laravel’s array rule can also limit an array’s keys, the names used to access its entries. For example, array:name,username limits the accepted keys; it does not check the values stored under those keys. To check each value, use a wildcard rule such as user.*. The rules look similar, but they check different things:

Rule or pattern What it checks
Rule::in([...]) Whether a value appears in a fixed list
in_array:another_field.* Whether a value appears among another field’s submitted values
array:name,username Whether an array uses only the listed keys
airports.* with Rule::in([...]) Whether each array element appears in a fixed list

The documentation says that in_array checks whether a value appears in another field, but it does not establish the exact behavior for every combination of input types. If your validation depends on whether a string and an integer count as the same value, check the behavior in the Laravel version your project uses.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *