From 2be0fe9b63f7b931a798f4b41e7561be6dfecd8b Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 22 Feb 2020 23:13:53 +0100 Subject: Allow form validation without marking fields with errors. --- src/main/kotlin/pl/treksoft/kvision/form/Form.kt | 17 ++++++++++------- src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt | 5 +++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt index 1c17999d..5e49e1b4 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt @@ -325,9 +325,10 @@ class Form( /** * Invokes validator function and validates the form. + * @param markFields determines if form fields should be labeled with error messages * @return validation result */ - fun validate(): Boolean { + fun validate(markFields: Boolean = true): Boolean { val fieldWithError = fieldsParams.mapNotNull { entry -> fields[entry.key]?.let { control -> @Suppress("UNCHECKED_CAST") @@ -335,14 +336,16 @@ class Form( val required = fieldsParams?.required ?: false val requiredError = control.getValue() == null && control.visible && required if (requiredError) { - control.validatorError = trans(fieldsParams?.requiredMessage) ?: "Value is required" + if (markFields) control.validatorError = trans(fieldsParams?.requiredMessage) ?: "Value is required" true } else { - val validatorPassed = fieldsParams?.validator?.invoke(control) ?: true - control.validatorError = if (!validatorPassed) { - trans(fieldsParams?.validatorMessage?.invoke(control)) ?: "Invalid value" - } else { - null + val validatorPassed = control.visible && (fieldsParams?.validator?.invoke(control) ?: true) + if (markFields) { + control.validatorError = if (!validatorPassed) { + trans(fieldsParams?.validatorMessage?.invoke(control)) ?: "Invalid value" + } else { + null + } } !validatorPassed } diff --git a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt index 77c324c5..ba144137 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt @@ -450,10 +450,11 @@ open class FormPanel( /** * Invokes validator function and validates the form. + * @param markFields determines if form fields should be labeled with error messages * @return validation result */ - open fun validate(): Boolean { - return form.validate() + open fun validate(markFields: Boolean = true): Boolean { + return form.validate(markFields) } companion object { -- cgit