From 3ecd9c222bb5dac30f460ed1848731b2661c0702 Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 5 Jan 2023 14:06:39 -0700 Subject: [PATCH] Fix switch widget did not validate value server-side --- api/js/etemplate/Et2Switch/Et2Switch.ts | 38 ++++++++++++------------- api/src/Etemplate/Widget/Checkbox.php | 13 +++++---- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/api/js/etemplate/Et2Switch/Et2Switch.ts b/api/js/etemplate/Et2Switch/Et2Switch.ts index 622e58f248..df3a0a26ee 100644 --- a/api/js/etemplate/Et2Switch/Et2Switch.ts +++ b/api/js/etemplate/Et2Switch/Et2Switch.ts @@ -87,32 +87,32 @@ export class Et2Switch extends Et2InputWidget(SlotMixin(SlSwitch)) this.toggleOff = ''; } - firstUpdated() + updated(changedProperties) { - if (!this.toggleOn && !this.toggleOff) + if(changedProperties.has("toggleOn") || changedProperties.has("toggleOff") || changedProperties.has("label")) { - this._labelNode.children().remove(); + if(!this.toggleOn && !this.toggleOff && this._labelNode) + { + this._labelNode.childNodes.forEach(c => c.remove()); + } + else + { + if(this._labelNode) + { + this._labelNode.querySelector('.on').textContent = this.toggleOn; + this._labelNode.querySelector('.off').textContent = this.toggleOff; + } + this.shadowRoot.querySelector('.switch__label').classList.add('toggle__label'); + } } - else - { - this._labelNode.querySelector('.on').textContent = this.toggleOn; - this._labelNode.querySelector('.off').textContent = this.toggleOff; - this.shadowRoot.querySelector('.switch__label').classList.add('toggle__label'); - } - } - - connectedCallback() - { - super.connectedCallback(); - } set value(new_value : string | boolean) { this.requestUpdate("checked"); - if (this.toggleOn || this.toggleOf) + if(this.toggleOn || this.toggleOf) { - if (new_value) + if(new_value) { this._labelNode?.classList.add('on'); this.checked = true; @@ -140,8 +140,8 @@ export class Et2Switch extends Et2InputWidget(SlotMixin(SlSwitch)) { return html` - - + ${this.toggleOn} + ${this.toggleOff} `; } diff --git a/api/src/Etemplate/Widget/Checkbox.php b/api/src/Etemplate/Widget/Checkbox.php index c633192c1a..cf7979c376 100644 --- a/api/src/Etemplate/Widget/Checkbox.php +++ b/api/src/Etemplate/Widget/Checkbox.php @@ -64,20 +64,20 @@ class Checkbox extends Etemplate\Widget self::set_validation_error($form_name,lang('Field must not be empty !!!'),''); } $type = $this->type ? $this->type : $this->attrs['type']; - $value_attr = $type == 'radio' ? 'set_value' : 'selected_value'; + $value_attr = $type == 'radio' ? 'set_value' : 'selectedValue'; // defaults for set and unset values $selected_value = true; $unselected_value = false; - if (array_key_exists($value_attr, $this->attrs) || array_key_exists('unselected_value',$this->attrs)) + if(array_key_exists($value_attr, $this->attrs) || array_key_exists('unselectedValue', $this->attrs)) { if(array_key_exists($value_attr, $this->attrs)) { // Expand any content stuff - $selected_value = self::expand_name($this->attrs[$value_attr], $expand['c'], $expand['row'], $expand['c_'], $expand['row_'],$expand['cont']); + $selected_value = self::expand_name($this->attrs[$value_attr], $expand['c'], $expand['row'], $expand['c_'], $expand['row_'], $expand['cont']); } - if(array_key_exists('unselectedValue',$this->attrs) || array_key_exists('unselected_value',$this->attrs)) + if(array_key_exists('unselectedValue', $this->attrs) || array_key_exists('unselected_value', $this->attrs)) { - $unselected_value = self::expand_name($this->attrs['unselectedValue'] ?? $this->attrs['unselected_value'], $expand['c'], $expand['row'], $expand['c_'], $expand['row_'],$expand['cont']); + $unselected_value = self::expand_name($this->attrs['unselectedValue'] ?? $this->attrs['unselected_value'], $expand['c'], $expand['row'], $expand['c_'], $expand['row_'], $expand['cont']); } } if ($type == 'radio') @@ -129,4 +129,5 @@ class Checkbox extends Etemplate\Widget } } } -Etemplate\Widget::registerWidget(__NAMESPACE__.'\\Checkbox', array('checkbox', 'radio')); \ No newline at end of file + +Etemplate\Widget::registerWidget(__NAMESPACE__ . '\\Checkbox', array('et2-checkbox', 'et2-radio', 'et2-switch')); \ No newline at end of file