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
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-30