Every Laravel developer has hit this at some point.
You’re working on a clean API. Everything looks good. Then suddenly validation fails because the frontend sent "true" instead of true.
Laravel’s default boolean rule is excellent for strictness, but it rejects common string values like "true" and "false" that come from JavaScript, React/Vue forms, query strings, or external services.
The result? Scattered filter_var() calls, custom rules, or manual casting — repeated across every project.
I got tired of the same workaround and decided to fix it once and for all.
Introducing flexible-boolean
I built a small, focused package that extends Laravel’s boolean validation to support real-world API inputs while keeping everything clean and secure.
Supported values now include:
- "true" / "false" (strings)
- 1 / 0
- Native true / false
It integrates seamlessly with Laravel’s validation system — no side effects, no performance hit.
Quick Example
Install via Composer:
composer require alihaider/flexible-boolean
Then use it in your Form Requests:
public function rules(): array
{
return [
'is_active' => ['required', 'boolean:flexible'],
'newsletter' => new FlexibleBooleanRule(),
'is_published' => 'boolean', // still works as before
];
}
You can also register the rule globally if you prefer.
Before vs After
Before: Random 422 errors + extra controller logic
After: Predictable validation that just works with frontend data
It’s a tiny package, but it saves a surprising amount of debugging time when building production APIs.
Let’s Discuss
How are you handling boolean values from the frontend in your Laravel projects today?
- Manual casting?
- Custom rules per project?
- Something else entirely?
Drop your preferred approach in the comments — I’m curious to see the different solutions the community uses.
Package
https://packagist.org/packages/alihaider/flexible-boolean
If you found this useful, give it a like or save it for later. Happy coding!
Top comments (1)
I came across this package while dealing with Laravel’s default boolean validation limitations, especially with string values like "true" and "false".
I must say, this is a very clean and practical solution to a real-world problem. The implementation is simple, effective, and integrates seamlessly without adding unnecessary complexity.
It saved me time and made my validation logic much more reliable and readable. Really appreciate the effort put into creating and maintaining this great work.