-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] x-adminlte-input-switch component: boolean error with Laravel 11 #1316
Comments
Hello @Figoware , the /**
* 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. |
@Figoware would you test the minor modification proposed in the previous PR? Your feedback about this will be useful. You just need to edit and apply the change in your |
Yes, it's working, and it's a nice/elegant solution, thank you! BTW, what's the best solution to handle "checked" attribute at init. Tried |
I don't think If you want to setup the initial state of the input-switch based on some @php
$config = [
'state' => /* The condition to evaluate (true or false) */,
];
@endphp
<x-adminlte-input-switch name="my_checkbox" label="My Checkbox" :config="$config"/> The underlying plugin configurations are documented here |
Thank you for the solution! @section('input_group_item')
{{-- Input Switch --}}
<input type="checkbox" id="{{ $id }}" name="{{ $name }}" @checked($getOldValue($errorKey,$attributes['state']??false))
{{ $attributes->merge(['class' => $makeItemClass(), 'value' => 'true' ]) }}
@overwrite and using Hope it can help someone! |
@Figoware good one, take into consideration that changes inside the |
Sure! (I've read the doc ^^) |
Let's keep this open, I will try to add a new attribute called |
Great! BTW, there is a bug in Bootstrap Switch: if checked at first page render, submiting the form won't include the checkbox in fields values... Or I am missing something. |
I ran some other tests: Here is what is really working for me + new feature (add unchecked support, i.e. see here): Undo your PR change. {{-- Input Switch --}}
@if($enabledUnchecked) <input type="hidden" name="{{ $name }}" value="0"/>@endif
<input type="checkbox" id="{{ $id }}" name="{{ $name }}"
{{ $attributes->merge(['class' => $makeItemClass(), 'value' => 'true', (($attributes['isChecked']?? false) ? 'checked': '') => '']) }}
> In public $enableUnchecked = false;
/**
* Create a new component instance.
* Note this component requires the 'Bootstrap Switch' plugin.
*
* @return void
*/
public function __construct(
$name, $id = null, $label = null, $igroupSize = null, $labelClass = null,
$fgroupClass = null, $igroupClass = null, $disableFeedback = null,
$errorKey = null, $config = [], $enableUnchecked = null,
$enableOldSupport = null,
) {
parent::__construct(
$name, $id, $label, $igroupSize, $labelClass, $fgroupClass,
$igroupClass, $disableFeedback, $errorKey
);
if ($enableUnchecked) $this->enableUnchecked = true;
$this->config = is_array($config) ? $config : [];
$this->enableOldSupport = isset($enableOldSupport);
} Usage : <x-adminlte-input-switch name="myBool" enable-old-support enableUnchecked isChecked="{{ $myBool }}" value="1" /> How does it sound for you? |
@Figoware Oh my gosh, it seems the plugin do not work nicely when setting the state on initialization procedure. I just added a workaround to address this, could you check changes on #1317 again. Also, for what I've seen, the version provided by AdminLTE of this plugin is the most stable ( |
Hi @Figoware , thanks for your time invested here, but I don't want to over complicate things with the enableUnchecked option. A checkbox only submits a value when it's checked. So you can detect that in the controller as I have explained before. Or if you want to always submit a value, you can still add the hidden input before the <input type="hidden" name="myBool" value="0"/>
<x-adminlte-input-switch name="myBool" enable-old-support is-checked="{{ $myBool }}" value="1" /> |
enableUncheck: I understand. But the Thank you @dfsmania! |
Sorry, I miss this one! Now, I'll try version 3.3.4 of adminLTE, thanks for pointing that out! |
Sorry for the misunderstood, I tried to point the most stable version of bootstrap-switch plugin, it's the one currently available on the AdminLTE distribution files and the current version we use here if you have followed the normal setup procedure of the plugin. This note must only be taken into consideration if you are using CDN files to include the bootstrap-switch plugin into your project. |
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
Laravel 11
Working solution
In
vendor\laravel\framework\src\Illuminate\Validation\Concerns\ValidatesAttributes.php
, changevalue="true"
byvalue="1"
: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!
The text was updated successfully, but these errors were encountered: