Laravel-AdminLTE: [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
public function validateBoolean($attribute, $value)
{
$acceptable = [true, false, 0, 1, '0', '1'];
return in_array($value, $acceptable, true);
}
Laravel 11
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":
@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!
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17
Let’s keep this open, I will try to add a new attribute called
is-checkedto implement the same functionality you did with thestateone.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.