diff options
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/form/Form.kt | 17 | ||||
-rw-r--r-- | 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<K : Any>( /** * 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<K : Any>( 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<K : Any>( /** * 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 { |