alihaider/flexible-boolean

Flexible boolean validation rule supporting true/false strings and values

Maintainers

Package info

github.com/Alihaider8014/laravel-flexible-boolean

pkg:composer/alihaider/flexible-boolean

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.3 2026-03-31 14:40 UTC

This package is auto-updated.

Last update: 2026-05-01 00:11:53 UTC


README

A powerful and flexible boolean validation rule for Laravel that supports real-world API and frontend inputs like "true" and "false".

๐Ÿš€ Why This Package Exists

Laravel's default boolean validation rule does NOT accept string values like:

  • "true"
  • "false"

However, in real-world applications (APIs, frontend forms, third-party integrations), boolean values are often sent as strings.

This package solves that problem by providing a flexible and reliable boolean validation rule.

โœจ Features

  • Supports:
    • true / false
    • "true" / "false"
    • 1 / 0
    • "1" / "0"
  • Laravel validation rule
  • Helper methods for conversion
  • Fully testable and production ready

๐Ÿ“ฆ Installation

Install via Composer:

composer require AliHaider/laravel-flexible-boolean

โš™๏ธ Service Provider

Laravel supports auto-discovery, so no manual registration is required.

If you are using an older Laravel version or need to register manually, add the service provider:

// config/app.php

'providers' => [
    AliHaider\FlexibleBoolean\FlexibleBooleanRuleServiceProvider::class,
];

โœ… Usage

1. Validation Rule

$request->validate([
    'is_active' => 'required|flexible_boolean'
]);

2. Using Rule Class

use AliHaider\FlexibleBoolean\FlexibleBooleanRule;

$request->validate([
    'is_active' => ['required', new FlexibleBooleanRule()],
]);

3. Convert Value to Boolean

use AliHaider\FlexibleBoolean\FlexibleBooleanRule;

FlexibleBooleanRule::toBoolean('true');  // true
FlexibleBooleanRule::toBoolean('false'); // false
FlexibleBooleanRule::toBoolean('1');     // true
FlexibleBooleanRule::toBoolean('0');     // false

โš–๏ธ Comparison with Laravel Default

Feature Laravel Default This Package
Accept "true" string โŒ No โœ… Yes
Accept "false" string โŒ No โœ… Yes
Strict boolean validation โœ… Yes โœ… Yes
API-friendly โŒ Limited โœ… Fully

๐Ÿงช Example (Real-World API)

POST /api/user

{
    "is_active": "true"
}

Validation:

$request->validate([
    'is_active' => 'required|flexible_boolean'
]);