定制 alihaider/flexible-boolean 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alihaider/flexible-boolean

最新稳定版本:v1.0.3

Composer 安装命令:

composer require alihaider/flexible-boolean

包简介

Flexible boolean validation rule supporting true/false strings and values

README 文档

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'
]);

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固