Fix switch widget did not validate value server-side

This commit is contained in:
nathan 2023-01-05 14:06:39 -07:00
parent 233b2f472c
commit 3ecd9c222b
2 changed files with 26 additions and 25 deletions

View File

@ -87,32 +87,32 @@ export class Et2Switch extends Et2InputWidget(SlotMixin(SlSwitch))
this.toggleOff = ''; 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) set value(new_value : string | boolean)
{ {
this.requestUpdate("checked"); 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._labelNode?.classList.add('on');
this.checked = true; this.checked = true;
@ -140,8 +140,8 @@ export class Et2Switch extends Et2InputWidget(SlotMixin(SlSwitch))
{ {
return html` return html`
<span class="label" aria-label="${this.label}"> <span class="label" aria-label="${this.label}">
<span class="on"></span> <span class="on">${this.toggleOn}</span>
<span class="off"></span> <span class="off">${this.toggleOff}</span>
</span> </span>
`; `;
} }

View File

@ -64,20 +64,20 @@ class Checkbox extends Etemplate\Widget
self::set_validation_error($form_name,lang('Field must not be empty !!!'),''); self::set_validation_error($form_name,lang('Field must not be empty !!!'),'');
} }
$type = $this->type ? $this->type : $this->attrs['type']; $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 // defaults for set and unset values
$selected_value = true; $selected_value = true;
$unselected_value = false; $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)) if(array_key_exists($value_attr, $this->attrs))
{ {
// Expand any content stuff // 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') if ($type == 'radio')
@ -129,4 +129,5 @@ class Checkbox extends Etemplate\Widget
} }
} }
} }
Etemplate\Widget::registerWidget(__NAMESPACE__.'\\Checkbox', array('checkbox', 'radio'));
Etemplate\Widget::registerWidget(__NAMESPACE__ . '\\Checkbox', array('et2-checkbox', 'et2-radio', 'et2-switch'));