<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ali Haider</title>
    <description>The latest articles on Forem by Ali Haider (@alihaider8014).</description>
    <link>https://forem.com/alihaider8014</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3852313%2F4bc86982-f568-45cf-8015-8c22430707d3.jpeg</url>
      <title>Forem: Ali Haider</title>
      <link>https://forem.com/alihaider8014</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alihaider8014"/>
    <language>en</language>
    <item>
      <title>Flexible Boolean Validation for Laravel APIs — Handling “true”/“false” Strings Without the Headache</title>
      <dc:creator>Ali Haider</dc:creator>
      <pubDate>Mon, 30 Mar 2026 20:52:18 +0000</pubDate>
      <link>https://forem.com/alihaider8014/flexible-boolean-validation-for-laravel-2oad</link>
      <guid>https://forem.com/alihaider8014/flexible-boolean-validation-for-laravel-2oad</guid>
      <description>&lt;p&gt;Every Laravel developer has hit this at some point.&lt;/p&gt;

&lt;p&gt;You’re working on a clean API. Everything looks good. Then suddenly validation fails because the frontend sent "true" instead of true.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The result? Scattered filter_var() calls, custom rules, or manual casting — repeated across every project.&lt;/p&gt;

&lt;p&gt;I got tired of the same workaround and decided to fix it once and for all.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Introducing flexible-boolean&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I built a small, focused package that extends Laravel’s boolean validation to support real-world API inputs while keeping everything clean and secure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Supported values now include:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"true" / "false" (strings)&lt;/li&gt;
&lt;li&gt;1 / 0&lt;/li&gt;
&lt;li&gt;Native true / false&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It integrates seamlessly with Laravel’s validation system — no side effects, no performance hit.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Quick Example&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Install via Composer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require alihaider/flexible-boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then use it in your Form Requests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'is_active'&lt;/span&gt;     &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'required'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'boolean:flexible'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="s1"&gt;'newsletter'&lt;/span&gt;    &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FlexibleBooleanRule&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="s1"&gt;'is_published'&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'boolean'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// still works as before&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also register the rule globally if you prefer.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Before vs After&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Before&lt;/strong&gt;: Random 422 errors + extra controller logic&lt;br&gt;
&lt;strong&gt;After&lt;/strong&gt;: Predictable validation that just works with frontend data&lt;br&gt;
It’s a tiny package, but it saves a surprising amount of debugging time when building production APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Let’s Discuss&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;How are you handling boolean values from the frontend in your Laravel projects today?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manual casting?&lt;/li&gt;
&lt;li&gt;Custom rules per project?&lt;/li&gt;
&lt;li&gt;Something else entirely?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Drop your preferred approach in the comments — I’m curious to see the different solutions the community uses.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Package&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://packagist.org/packages/alihaider/flexible-boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you found this useful, give it a like or save it for later. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>php</category>
      <category>laravel</category>
      <category>api</category>
    </item>
  </channel>
</rss>
