Skip to content
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

Closed
siliconalchemy opened this issue Jan 14, 2015 · 12 comments
Closed

switchinput won't show Off/false #219

siliconalchemy opened this issue Jan 14, 2015 · 12 comments

Comments

@siliconalchemy
Copy link

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.

@kartik-v
Copy link
Owner

You need to check your data and settings. For an example of off and on states on init - refer the documentation and demo.

@siliconalchemy
Copy link
Author

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.

@kartik-v
Copy link
Owner

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.

@siliconalchemy
Copy link
Author

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.
Hope that's as clear as mud..

@siliconalchemy
Copy link
Author

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':
if (!array_key_exists('value', $options)) {
$options['value'] = '1';
}
This is somewhat odd behaviour, granted, but because your InputWidget::getInput doesn't set this specifically it gets passed back as '1' which is then I assume interpreted by Switchinput as 'On'.

@kartik-v
Copy link
Owner

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.

@kartik-v
Copy link
Owner

just also ensure you have the updated version of all packages from composer... cross check yii2-krajee-base as well

@siliconalchemy
Copy link
Author

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.

@kartik-v
Copy link
Owner

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.

@kartik-v
Copy link
Owner

You can reinitalize it faster without actually remembering the option - since there is a data property data-krajee-bootstrapSwitch that is added to each base input which contains the plugin settings.

So if your input id is = chk you can call the following JS code after tab is shown:

function reinitialize() {
    var $el = $('#chk'), opts = $el.attr('data-krajee-bootstrapSwitch');
    $el.bootstrapSwitch(opts);
}

@siliconalchemy
Copy link
Author

I think i'm just using bootstrap tabs. Apparently it's a bug in bootstrapSwitch:
Bttstrp/bootstrap-switch#392
I've replaced the assets in your widget with 3.3.1 and it works fine now :)

@kartik-v
Copy link
Owner

I will record an enhancement to update the assets.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants