aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2020-02-22 23:13:53 +0100
committerRobert Jaros <rjaros@finn.pl>2020-02-22 23:13:53 +0100
commit2be0fe9b63f7b931a798f4b41e7561be6dfecd8b (patch)
tree27f9b1be4282014c58070bdff6d9e1b13146cb24
parent372d60cf3b4917cefd3f6bb59f9ae5c766f14e36 (diff)
downloadkvision-2be0fe9b63f7b931a798f4b41e7561be6dfecd8b.tar.gz
kvision-2be0fe9b63f7b931a798f4b41e7561be6dfecd8b.tar.bz2
kvision-2be0fe9b63f7b931a798f4b41e7561be6dfecd8b.zip
Allow form validation without marking fields with errors.
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/Form.kt17
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/FormPanel.kt5
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 {