Repository: jeroennoten/Laravel-AdminLTE (language: php)

Issue:
[BUG] x-adminlte-input-switch component: boolean error with Laravel 11
### Describe the bug
Unable to use `<x-adminlte-input-switch>`: string "true" is not allowed anymore in Laravel 11.
Even by using valdator `string|in:true,false` did not works (string value "true" won't be converted in boolean, causing DB insert error).

### Changed in Laravel
File `vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php` changed in Laravel 11.
#### Laravel <=10
```php
   public function validateBoolean($attribute, $value)
    {
        $acceptable = [true, false, 0, 1, '0', '1'];

        return in_array($value, $acceptable, true);
    }
```
#### Laravel 11
```php
    public function validateBoolean($attribute, $value)
    {
        $acceptable = [true, false, 0, 1, '0', '1'];

        return in_array($value, $acceptable, true);
    }
```
### Working solution
In `vendor\laravel\framework\src\Illuminate\Validation\Concerns\ValidatesAttributes.php`, change  `value="true"` by `value="1"`:
```html
@section('input_group_item')

    {{-- Input Switch --}}
    <input type="checkbox" id="{{ $id }}" name="{{ $name }}" value="1"
        {{ $attributes->merge(['class' => $makeItemClass()]) }}>

@overwrite
```

### Useful links
https://stackoverflow.com/questions/61713836/laravels-validation-failed-for-boolean-value-sended-by-postman
https://echebaby.com/blog/2021-12-30-laravel-validate-true-and-false-as-booleans/


Thank you!


Discussion / hints:
Hello @Figoware , the `<x-adminlte-input-switch>` is just a nice checkbox, so when submitting the value  you would check whether the field name is present in the request to act in consequence (and yes, it's currently submitting the string `'true'` when present). I mostly use it like this with validations, use it as reference:

```php
/**
 * Store a newly created resource in storage.
 */
public function store(Request $request): RedirectResponse
{
    // Map some request data to their correct values.

    $request->merge([
        'checkbox_name' => $request->has('checkbox_name'),
    ]);

    // Now, perfom validations on the submitted data.

    $validatedData = $request->validate([
        'other_field' => 'required|string|size:14',
        'checkbox_name' => 'bool',
    ]);

    // Other code...
    ...
}
```

So, if you still think there's a problem or issue with the component, please share the details about it.
Thank you @dfsmania!

Sure, there are workarounds. But it's not as elegant as possible (in my opinion)!

The best would be to change the value when checked to 1, or to add a option in the component for checked value...

BTW, sorry for the [BUG] tag, my mistake, obviously it's not a bug.



Fix patch:
diff --git a/resources/views/components/form/input-switch.blade.php b/resources/views/components/form/input-switch.blade.php
index 974647dc..6c9c3d38 100644
--- a/resources/views/components/form/input-switch.blade.php
+++ b/resources/views/components/form/input-switch.blade.php
@@ -9,8 +9,8 @@
 @section('input_group_item')
 
     {{-- Input Switch --}}
-    <input type="checkbox" id="{{ $id }}" name="{{ $name }}" value="true"
-        {{ $attributes->merge(['class' => $makeItemClass()]) }}>
+    <input type="checkbox" id="{{ $id }}" name="{{ $name }}"
+        {{ $attributes->merge(['class' => $makeItemClass(), 'value' => 'true']) }}>
 
 @overwrite
 
@@ -20,7 +20,13 @@
 <script>
 
     $(() => {
-        $('#{{ $id }}').bootstrapSwitch( @json($config) );
+
+        let usrCfg = @json($config);
+        $('#{{ $id }}').bootstrapSwitch(usrCfg);
+
+        // Workaround to ensure correct state setup on initialization.
+
+        $('#{{ $id }}').bootstrapSwitch('state', usrCfg.state ?? false);
 
         // Add support to auto select the previous submitted value in case of
         // validation errors.
diff --git a/src/View/Components/Form/InputSwitch.php b/src/View/Components/Form/InputSwitch.php
index 3db1fe73..c960bfbf 100644
--- a/src/View/Components/Form/InputSwitch.php
+++ b/src/View/Components/Form/InputSwitch.php
@@ -24,7 +24,8 @@ class InputSwitch extends InputGroupComponent
     public function __construct(
         $name, $id = null, $label = null, $igroupSize = null, $labelClass = null,
         $fgroupClass = null, $igroupClass = null, $disableFeedback = null,
-        $errorKey = null, $config = [], $enableOldSupport = null
+        $errorKey = null, $config = [], $isChecked = null,
+        $enableOldSupport = null
     ) {
         parent::__construct(
             $name, $id, $label, $igroupSize, $labelClass, $fgroupClass,
@@ -32,6 +33,11 @@ public function __construct(
         );
 
         $this->config = is_array($config) ? $config : [];
+
+        if (isset($isChecked)) {
+            $this->config['state'] = ! empty($isChecked);
+        }
+
         $this->enableOldSupport = isset($enableOldSupport);
     }
 


Test patch:
diff --git a/tests/Components/FormComponentsTest.php b/tests/Components/FormComponentsTest.php
index c246c3ea..429ac7de 100644
--- a/tests/Components/FormComponentsTest.php
+++ b/tests/Components/FormComponentsTest.php
@@ -408,7 +408,39 @@ public function testInputSliderComponentOldSupport()
     |--------------------------------------------------------------------------
     */
 
-    public function testInputSwitchComponent()
+    public function testInputSwitchComponentCheckedState()
+    {
+        // Test the state property isn't defined when is-checked attribute
+        // isn't provided.
+
+        $component = new Components\Form\InputSwitch('name');
+
+        $this->assertArrayNotHasKey('state', $component->config);
+
+        // Test the state property is true when is-checked attribute has a
+        // truthy value.
+
+        foreach ([true, 1, 'true'] as $v) {
+            $component = new Components\Form\InputSwitch(
+                'name', null, null, null, null, null, null, null, null, null, $v
+            );
+
+            $this->assertTrue($component->config['state']);
+        }
+
+        // Test the state property is false when is-checked attribute has a
+        // falsy value.
+
+        foreach ([false, 0, ''] as $v) {
+            $component = new Components\Form\InputSwitch(
+                'name', null, null, null, null, null, null, null, null, null, $v
+            );
+
+            $this->assertFalse($component->config['state']);
+        }
+    }
+
+    public function testInputSwitchComponentWithErrorStyle()
     {
         $component = new Components\Form\InputSwitch(
             'name', null, null, 'lg', null, null, 'igroup-class'
@@ -438,7 +470,7 @@ public function testInputSwitchComponentOldSupport()
         // Test component with old support enabled.
 
         $component = new Components\Form\InputSwitch(
-            'name', null, null, null, null, null, null, null, null, null, true
+            'name', null, null, null, null, null, null, null, null, null, null, true
         );
 
         $this->addInputOnCurrentRequest('name', 'foo');