From db83cc437cb2824d68100387508f15d8cf547942 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Thu, 30 Sep 2021 09:26:36 +0200 Subject: [PATCH] refactor client-side form is not valid/submittable check of submit method into an own method isInvalid() --- api/js/etemplate/etemplate2.ts | 45 +++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/api/js/etemplate/etemplate2.ts b/api/js/etemplate/etemplate2.ts index 43b417a31a..af193ebf9a 100644 --- a/api/js/etemplate/etemplate2.ts +++ b/api/js/etemplate/etemplate2.ts @@ -839,6 +839,38 @@ export class etemplate2 } } + /** + * Check if there is an invalid widget / all widgets are valid + * + * @param container + * @param values + * @return et2_widget|null first invalid widget or null, if all are valid + */ + isInvalid(container : et2_container|undefined, values : object|undefined) : et2_widget|null + { + if (typeof container === 'undefined') + { + container = this._widgetContainer; + } + if (typeof values === 'undefined') + { + values = this.getValues(container); + } + let invalid = null; + container.iterateOver(function (_widget) + { + if (_widget.submit(values) === false) + { + if(!invalid && !_widget.isValid([])) + { + invalid = _widget; + } + } + }, this, et2_ISubmitListener); + + return invalid; + } + /** * Submit form via ajax * @@ -867,17 +899,7 @@ export class etemplate2 let invalid = null; if (!no_validation) { - container.iterateOver(function (_widget) - { - if (_widget.submit(values) === false) - { - if(!invalid && !_widget.isValid()) - { - invalid = _widget; - } - canSubmit = false; - } - }, this, et2_ISubmitListener); + canSubmit = !(invalid = this.isInvalid(container, values)); } if (canSubmit) @@ -1082,7 +1104,6 @@ export class etemplate2 return result; } - /** * "Intelligently" refresh the template based on the given ID *