boolean

Validates that a field can be cast to a boolean, accepting only true, false, 1, 0, "1" and "0".

Signature
boolean  |  boolean:strict

Parameters

ParameterTypeDescription
$strictoptionalstringLiteral flag. When present, only the values true and false pass; 1, 0, "1" and "0" are rejected.

Errors

Nothing on its own — the failure becomes a message, and validate() on a failing validator throws Illuminate\Validation\ValidationException.

Changelog

  • PHP 12.xAdded the strict parameter (boolean:strict), which accepts only literal true and false.

boolean asserts that a field can be cast to a boolean. The accepted set is exact and short: true, false, 1, 0, "1" and "0". Nothing else passes — not "yes", not "on", not "true", not null, not the empty string.

When to reach for it

  • Checkbox and toggle fields that map to a tinyint or boolean column.
  • Flags in a JSON API payload, such as {"notify": true}.
  • Feature switches on a settings form, usually with sometimes or nullable.

What trips people up

An unchecked HTML checkbox is not submitted at all, so a rule of ['required', 'boolean'] fails the entire form the moment a user leaves the box unticked. Use ['sometimes', 'boolean'] or ['nullable', 'boolean'] and default the value yourself, or post a hidden 0 before the checkbox.

A checked checkbox also submits "on" unless you set value="1", and "on" is not in the accepted set. Finally, the rule does not convert anything: validated() still returns the string "1", which is truthy, but "0" is also truthy in PHP. Read the value with $request->boolean('notify') or cast the attribute on the model.

Example

Example · php
<?php

use Illuminate\Support\Facades\Validator;

$validator = Validator::make([
    'notify'    => '1',
    'subscribe' => 'yes',
], [
    'notify'    => ['required', 'boolean'],
    'subscribe' => ['required', 'boolean'],
]);

print_r($validator->errors()->toArray());

// Array
// (
//     [subscribe] => Array
//         (
//             [0] => The subscribe field must be true or false.
//         )
// )

When to use it

  • Validate a marketing-opt-in checkbox on a registration form.
  • Accept a notify flag in a JSON API request body.
  • Guard a settings toggle that writes to a boolean database column.
  • Require explicit consent with accepted rather than boolean when the value must be true.

Frequently asked questions

Why does "yes" or "on" fail Laravel's boolean validation?

Because the rule accepts only true, false, 1, 0, "1" and "0" — nothing else. A checked checkbox submits "on" unless you set value="1" on the input, so either fix the markup or normalise the value in prepareForValidation().

How do I validate a checkbox that is sometimes unchecked?

Use ['sometimes', 'boolean'] or ['nullable', 'boolean'] instead of required, because an unchecked checkbox is not submitted at all. Then read the result with $request->boolean('field'), which returns false when the key is missing.

Does the boolean rule convert "0" into false?

No. validated() still returns the string "0", which is truthy in an if statement. Use $request->boolean('field'), an explicit (bool) cast on a properly normalised value, or a 'boolean' cast on the Eloquent model.

More examples

The unchecked checkbox problem

sometimes runs the rule only when the key is present, so an unticked box is simply absent instead of a validation error. preferences() then normalises both fields to real PHP booleans with $request->boolean(), which maps "1", "true", "on" and "yes" to true and everything else to false.

Example · php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdatePreferencesRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            // 'required' would fail whenever the box is left unticked,
            // because an unchecked checkbox is never submitted.
            'newsletter' => ['sometimes', 'boolean'],
            'digest'     => ['nullable', 'boolean'],
        ];
    }

    public function preferences(): array
    {
        return [
            'newsletter' => $this->boolean('newsletter'),
            'digest'     => $this->boolean('digest'),
        ];
    }
}

Accepted values, one by one

The complete accepted set. Note that the string "true" and the checkbox default "on" both fail — give your checkbox inputs an explicit value="1" attribute, or normalise the payload in prepareForValidation() before the rules run.

Example · php
<?php

use Illuminate\Support\Facades\Validator;

$candidates = [true, false, 1, 0, '1', '0', 'true', 'on', 'yes', '', null];

foreach ($candidates as $value) {
    $passes = Validator::make(
        ['flag' => $value],
        ['flag' => 'boolean']
    )->passes();

    echo str_pad(var_export($value, true), 8), $passes ? "pass\n" : "fail\n";
}

// true    pass
// false   pass
// 1       pass
// 0       pass
// '1'     pass
// '0'     pass
// 'true'  fail
// 'on'    fail
// 'yes'   fail
// ''      fail
// NULL    fail

Strict boolean for a typed client

Under strict only literal true and false are accepted, so the JSON 1 fails even though the plain boolean rule would let it through. Use it on typed API clients you control; a browser form can never satisfy it because HTML input arrives as "1" and "0".

Example · php
<?php

use Illuminate\Support\Facades\Validator;

$payload = json_decode('{"archived": true, "pinned": 1}', true);

$validator = Validator::make($payload, [
    'archived' => ['boolean:strict'],
    'pinned'   => ['boolean:strict'],
]);

print_r($validator->errors()->keys());

// Array
// (
//     [0] => pinned
// )

Discussion

  • Be the first to comment on this lesson.