-
-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
switchinput won't show Off/false #219
Comments
You need to check your data and settings. For an example of off and on states on init - refer the documentation and demo. |
This exact code used to work just fine, an update of your component broke it. On the documentation page you refer to, there's a comment by Pierre M which describes the same thing. I'm seeing the same symptoms as him, I see a hidden field generated with the value 0, then the switch field generated with the value 1. I've checked that I've got the latest 1.20 code and it's being used. |
The Switchinput uses a yii generated checkbox input for intialization (the switch works like a checkbox). The Yii checkbox field internally generates the hidden input (you may want to check in yii docs about that). Anyway, since I cannot replicate your issue I would suggest you to check for details when your use case happens and what triggers your situation. The demo page contains examples of initializing switch inputs with initial data both via model and without model and it does work without any issues. |
Right so InputWidget::getInput asks Html::activeCheckbox for the markup which seems to always return (in my case, anyway) a hidden field with value 0 and a checkbox field with value 1, regardless of the value of the actual model field. The difference is that if the actual field data is 1, it returns 'checked' in the markup, and if the field data is 0, it doesn't return checked. A normal checkbox thus renders as expected because it uses the 'checked' value, whereas the switch input seems to use the 'value' value. |
It seems the Html::activeCheckbox deliberately writes the true value of the field into the hidden field, so it can be passed back to the server and saved into the model correctly as a fallback. However, for rendering the checkbox field it specifically looks for a 'value' key in $options and if not set it sets it as '1': |
Irrespective of the hidden input (its not needed for the plugin) - because the switchinput jquery plugin looks at checkbox and its state only. The hidden input is used by yii (its just an input with same name as the checkbox) - so that you can get the value in $_POST in php on your form post for loading into the model. However the plugin is initialized on input id and not the input name attribute. So its always the checkbox which is read by the plugin. You may need to debug and find out what's happening? The checkbox as generated by Yii should automatically set the checked state based on the model value. As I said it works on the demo pages. |
just also ensure you have the updated version of all packages from composer... cross check yii2-krajee-base as well |
Ok, i note that there's another comment by 'Infoweb' on your switchinput page that says his switch doesn't work if it's not on the initial tab. I've just noticed I've got the same problem - multiple switches work on the first tab, but they're all 'On' in the second tab. |
Ah I see you are using it in another plugin Tabs-X. See, this is a problem unique to the core plugin - it does not initialize the plugin until the input is visible. This was a problem on SwitchInputs getting rendered in DetailView as well for which I had to fix DetailView - you basically need to re-initialize the plugin on each tab load after the inputs are visible. |
You can reinitalize it faster without actually remembering the option - since there is a data property So if your input id is = function reinitialize() {
var $el = $('#chk'), opts = $el.attr('data-krajee-bootstrapSwitch');
$el.bootstrapSwitch(opts);
} |
I think i'm just using bootstrap tabs. Apparently it's a bug in bootstrapSwitch: |
I will record an enhancement to update the assets. |
Switchinput never shows off value. I'm using standard activefield widget:
field($user->uxprefs, 'menu_collapsed')->widget(SwitchInput::classname()); ?>It always shows On on page load. If you switch off and save, the value gets saved to 0 in the database but switch is still rendered as On.
The text was updated successfully, but these errors were encountered: