DEV Community

Ali Haider
Ali Haider

Posted on • Edited on

Flexible Boolean Validation for Laravel APIs — Handling “true”/“false” Strings Without the Headache

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
Enter fullscreen mode Exit fullscreen mode

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
    ];
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If you found this useful, give it a like or save it for later. Happy coding!

Top comments (1)

Collapse
 
hassan_khan_06 profile image
Hassan Khan

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.