From b91351c5edb9cf7bbf640c814a675432673f6acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3na=20Lore?= Date: Fri, 3 Mar 2017 00:03:31 +0100 Subject: [PATCH] Fix for unchecked-checkbox value submission. --- e107_handlers/form_handler.php | 6 ++--- e107_web/js/bootstrap.switch.init.js | 36 +++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/e107_handlers/form_handler.php b/e107_handlers/form_handler.php index 148e5943a..2f073c15f 100644 --- a/e107_handlers/form_handler.php +++ b/e107_handlers/form_handler.php @@ -2353,7 +2353,7 @@ class e_form 'size' => $options['switch'], 'onText' => $options_on['label'], 'offText' => $options_off['label'], - + 'inverse' => !empty($options['inverse']), ), ); @@ -2362,12 +2362,12 @@ class e_form $js_options[$name]['wrapperClass'] = 'wrapper form-control'; } - e107::library('load', 'bootstrap.switch'); e107::js('settings', array('bsSwitch' => $js_options)); e107::js('footer', '{e_WEB}js/bootstrap.switch.init.js', 'jquery', 5); - $text = $this->checkbox($name, 1, $checked_enabled); + $text = $this->hidden($name, (int) $checked_enabled); + $text .= $this->checkbox($name, 1, $checked_enabled); } elseif(!empty($options['inverse'])) // Same as 'writeParms'=>'reverse=1&enabled=LAN_DISABLED&disabled=LAN_ENABLED' { diff --git a/e107_web/js/bootstrap.switch.init.js b/e107_web/js/bootstrap.switch.init.js index c2bf19955..45ec53cac 100644 --- a/e107_web/js/bootstrap.switch.init.js +++ b/e107_web/js/bootstrap.switch.init.js @@ -18,12 +18,36 @@ var e107 = e107 || {'settings': {}, 'behaviors': {}}; { $('input[name="' + name + '"]', context).once('bootstrap-switch-init').each(function () { - $(this).bootstrapSwitch({ - size: options.size || 'mini', - onText: options.onText || null, - offText: options.offText || null, - wrapperClass: options.wrapperClass || null - }); + if($(this).attr('type') != 'hidden') + { + $(this).bootstrapSwitch({ + size: options.size || 'mini', + onText: options.onText || null, + offText: options.offText || null, + wrapperClass: options.wrapperClass || null + }); + + $(this).on('switchChange.bootstrapSwitch', function (event, state) { + var name = $(this).attr('name'); + var checked = true; + + if(state === false) + { + checked = false; + } + + var value = checked ? 1 : 0; + + if(options.inverse) + { + // value = checked ? 0 : 1; + } + + $('input[type="hidden"][name="' + name + '"]').val(value); + + event.preventDefault(); + }); + } }); }); }