From 374ba994a4db9c612a74ef34e9a4c69ba23dfc88 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 13 Jan 2023 11:28:02 -0700 Subject: [PATCH] Customfield validation fixes - customfield did not pass required setting on to widgets - link entry did not support required --- api/js/etemplate/Et2Link/Et2LinkEntry.ts | 34 ++++++++++++++++------- api/js/etemplate/Et2Link/Et2LinkSearch.ts | 5 ++++ api/src/Etemplate/Widget/Customfields.php | 4 +-- api/src/Etemplate/Widget/Link.php | 27 ++++++++++-------- 4 files changed, 46 insertions(+), 24 deletions(-) diff --git a/api/js/etemplate/Et2Link/Et2LinkEntry.ts b/api/js/etemplate/Et2Link/Et2LinkEntry.ts index 4b94ca0fad..873bd01e28 100644 --- a/api/js/etemplate/Et2Link/Et2LinkEntry.ts +++ b/api/js/etemplate/Et2Link/Et2LinkEntry.ts @@ -10,7 +10,7 @@ import {css, html, LitElement, PropertyValues, SlotMixin} from "@lion/core"; import {Et2LinkAppSelect} from "./Et2LinkAppSelect"; import {Et2InputWidget} from "../Et2InputWidget/Et2InputWidget"; -import {FormControlMixin, ValidateMixin} from "@lion/form-core"; +import {FormControlMixin} from "@lion/form-core"; import {Et2LinkSearch} from "./Et2LinkSearch"; import {Et2Link, LinkInfo} from "./Et2Link"; @@ -19,23 +19,25 @@ import {Et2Link, LinkInfo} from "./Et2Link"; * * */ -export class Et2LinkEntry extends Et2InputWidget(FormControlMixin(ValidateMixin(SlotMixin(LitElement)))) +export class Et2LinkEntry extends Et2InputWidget(FormControlMixin(SlotMixin(LitElement))) { static get styles() { return [ ...super.styles, css` - :host { + :host { display: block; - } - :host(.hideApp) ::slotted([slot="app"]) { + } + + :host(.hideApp) ::slotted([slot="app"]) { display: none; - } - .input-group__input { + } + + .input-group__input { gap: 0.5rem; - } - + } + ` ]; } @@ -155,6 +157,10 @@ export class Et2LinkEntry extends Et2InputWidget(FormControlMixin(ValidateMixin( updated(changedProperties : PropertyValues) { super.updated(changedProperties); + if(changedProperties.has("required")) + { + this._searchNode.required = this.required; + } if(changedProperties.has("readonly")) { this._appNode.readonly = this.readonly; @@ -266,6 +272,8 @@ export class Et2LinkEntry extends Et2InputWidget(FormControlMixin(ValidateMixin( this._searchNode.value = ""; this._searchNode.clearSearch(); this._searchNode.focus(); + + this.requestUpdate('value'); } /** @@ -277,6 +285,9 @@ export class Et2LinkEntry extends Et2InputWidget(FormControlMixin(ValidateMixin( { this.classList.add("hideApp"); this.dispatchEvent(new Event("change")); + this.requestUpdate('value'); + + this.validate(); } @@ -291,6 +302,9 @@ export class Et2LinkEntry extends Et2InputWidget(FormControlMixin(ValidateMixin( this._searchNode.focus(); this.dispatchEvent(new Event("change")); + this.requestUpdate('value'); + + this.validate(); } @@ -388,7 +402,7 @@ export class Et2LinkEntry extends Et2InputWidget(FormControlMixin(ValidateMixin( _inputGroupInputTemplate() { return html` -
+
diff --git a/api/js/etemplate/Et2Link/Et2LinkSearch.ts b/api/js/etemplate/Et2Link/Et2LinkSearch.ts index 037901bf2c..9c1d62e58c 100644 --- a/api/js/etemplate/Et2Link/Et2LinkSearch.ts +++ b/api/js/etemplate/Et2Link/Et2LinkSearch.ts @@ -133,6 +133,11 @@ export class Et2LinkSearch extends Et2Select } }); } + + public validate() + { + // Do not validate + } } // @ts-ignore TypeScript is not recognizing that this widget is a LitElement diff --git a/api/src/Etemplate/Widget/Customfields.php b/api/src/Etemplate/Widget/Customfields.php index 8674cf4dc6..3b8b612360 100644 --- a/api/src/Etemplate/Widget/Customfields.php +++ b/api/src/Etemplate/Widget/Customfields.php @@ -295,8 +295,8 @@ class Customfields extends Transformer $type = 'link-to'; } } - $xml = '<'.$type.' type="'.$type.'" id="'.self::$prefix.$fname.'"/>'; - $widget = self::factory($type, $xml, self::$prefix.$fname); + $xml = '<' . $type . ' type="' . $type . '" id="' . self::$prefix . $fname . '" required="' . $field['needed'] . '"/>'; + $widget = self::factory($type, $xml, self::$prefix . $fname); $widget->id = self::$prefix.$fname; $widget->attrs['type'] = $type; $widget->set_attrs($xml); diff --git a/api/src/Etemplate/Widget/Link.php b/api/src/Etemplate/Widget/Link.php index c4923afb81..cd78fd124a 100644 --- a/api/src/Etemplate/Widget/Link.php +++ b/api/src/Etemplate/Widget/Link.php @@ -358,10 +358,6 @@ class Link extends Etemplate\Widget * * Following attributes get checked: * - needed: value must NOT be empty - * - min, max: int and float widget only - * - maxlength: maximum length of string (longer strings get truncated to allowed size) - * - preg: perl regular expression incl. delimiters (set by default for int, float and colorpicker) - * - int and float get casted to their type * * @param string $cname current namespace * @param array $expand values for keys 'c', 'row', 'c_', 'row_', 'cont' @@ -377,23 +373,30 @@ class Link extends Etemplate\Widget $value = $value_in =& self::get_array($content, $form_name); // keep values added into request by other ajax-functions, eg. files draged into htmlarea (Vfs) - if ((!$value || is_array($value) && !$value['to_id']) && is_array($expand['cont'][$this->id]) && !empty($expand['cont'][$this->id]['to_id'])) + if((!$value || is_array($value) && !$value['to_id']) && is_array($expand['cont'][$this->id]) && !empty($expand['cont'][$this->id]['to_id'])) { - if (!is_array($value)) $value = array( - 'to_app' => $expand['cont'][$this->id]['to_app'], - ); + if(!is_array($value)) + { + $value = array( + 'to_app' => $expand['cont'][$this->id]['to_app'], + ); + } $value['to_id'] = $expand['cont'][$this->id]['to_id']; } - + if((string)$value === '' && $this->required) + { + self::set_validation_error($form_name, lang('Field must not be empty !!!'), ''); + return; + } // Link widgets can share IDs, make sure to preserve values from others - $already = self::get_array($validated,$form_name); + $already = self::get_array($validated, $form_name); if($already != null) { - $value = array_merge((array)$value, $already); + $value = array_merge((array)$value, $already); } // Automatically do link if user selected entry but didn't click 'Link' button $link = self::get_array($content, self::form_name($cname, $this->id . '_link_entry')); - if($this->type =='link-to' && is_array($link) && $link['app'] && $link['id'] ) + if($this->type == 'link-to' && is_array($link) && $link['app'] && $link['id']) { // Do we have enough information to link automatically? if(is_array($value) && $value['to_id'])