From ce84dd753ac0aed158837db70d2cd5eb44c11077 Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 24 Feb 2022 10:35:53 -0700 Subject: [PATCH] Get validation messages working --- .../Et2InputWidget/Et2InputWidget.ts | 25 ++++++++++++++++++- .../etemplate/Et2InputWidget/ManualMessage.ts | 17 +++++++++++++ package.json | 1 + 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 api/js/etemplate/Et2InputWidget/ManualMessage.ts diff --git a/api/js/etemplate/Et2InputWidget/Et2InputWidget.ts b/api/js/etemplate/Et2InputWidget/Et2InputWidget.ts index fafc46e07e..921d4ef546 100644 --- a/api/js/etemplate/Et2InputWidget/Et2InputWidget.ts +++ b/api/js/etemplate/Et2InputWidget/Et2InputWidget.ts @@ -1,6 +1,7 @@ import {et2_IInput, et2_IInputNode} from "../et2_core_interfaces"; import {Et2Widget} from "../Et2Widget/Et2Widget"; import {dedupeMixin} from "@lion/core"; +import {ManualMessage} from "./ManualMessage"; /** * This mixin will allow any LitElement to become an Et2InputWidget @@ -188,9 +189,31 @@ const Et2InputWidgetMixin = (superclass) => return this._inputNode; } + transformAttributes(attrs) + { + super.transformAttributes(attrs); + // Check whether an validation error entry exists + if(this.id && this.getArrayMgr("validation_errors")) + { + let val = this.getArrayMgr("validation_errors").getEntry(this.id); + if(val) + { + this.set_validation_error(val); + } + } + } + set_validation_error(err : string) { - // ToDo, just a stub to silence TypeErrors + // ToDo - implement Lion validators properly, most likely by adding to this.validators + + // Need to change interaction state so messages show up + this.prefilled = true; + // Add validator + this.validators.push(new ManualMessage(err)); + // Force a validate - not needed normally, but if you call set_validation_error() manually, + // it won't show up without validate() + this.validate(); } } diff --git a/api/js/etemplate/Et2InputWidget/ManualMessage.ts b/api/js/etemplate/Et2InputWidget/ManualMessage.ts new file mode 100644 index 0000000000..43df276adc --- /dev/null +++ b/api/js/etemplate/Et2InputWidget/ManualMessage.ts @@ -0,0 +1,17 @@ +import {ResultValidator} from "@lion/form-core"; + +/** + * Manual validator for server-side validation messages passed + * from Etemplate. It is always "activated", and just gives whatever + * message is passed in when created. + * + */ +export class ManualMessage extends ResultValidator +{ + static validatorName = "ManualMessage"; + + static async getMessage({fieldName, modelValue, formControl, params}) + { + return params; + } +} \ No newline at end of file diff --git a/package.json b/package.json index f14331e967..9fa7f644a7 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "@bundled-es-modules/pdfjs-dist": "^2.5.207-rc1", "@lion/button": "^0.14.2", "@lion/core": "^0.18.2", + "@lion/form-core": "^0.15.1", "@lion/input": "^0.15.4", "@lion/input-date": "^0.12.6", "@lion/input-datepicker": "^0.23.6",