From d54a970c34697e57d98321d5ce0e19bd18b63e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Bal=C3=A1k?= Date: Fri, 26 Jun 2020 00:47:51 +0200 Subject: [PATCH] FEAT: NullableEmailValidator --- formbuilderlibrary/build.gradle | 2 +- .../validator/NullableEmailValidator.kt | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/validator/NullableEmailValidator.kt diff --git a/formbuilderlibrary/build.gradle b/formbuilderlibrary/build.gradle index 45cf7a8..f6ab9a2 100644 --- a/formbuilderlibrary/build.gradle +++ b/formbuilderlibrary/build.gradle @@ -9,7 +9,7 @@ android { minSdkVersion 21 targetSdkVersion 28 versionCode 1 - versionName "7.4" + versionName "7.5" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/validator/NullableEmailValidator.kt b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/validator/NullableEmailValidator.kt new file mode 100644 index 0000000..9805121 --- /dev/null +++ b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/validator/NullableEmailValidator.kt @@ -0,0 +1,20 @@ +package cz.qase.android.formbuilderlibrary.validator + +import android.util.Patterns +import cz.qase.android.formbuilderlibrary.ValidationException + +/** + * Field validator + * + * Is ok when value is not null and not blank (aka empty or made only of whitespaces) + * see String.isNullOrBlank() + */ +class NullableEmailValidator(private val errorMsg: String) : FormValidator { + override fun validate(value: String?) { + if (!value.isNullOrBlank()) { + if (!Patterns.EMAIL_ADDRESS.matcher(value).matches()) { + throw ValidationException(errorMsg) + } + } + } +}