From 761b10c754dbef462d0da9a8df1c8e2aac38f6fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Bal=C3=A1k?= Date: Fri, 3 May 2019 13:10:28 +0200 Subject: [PATCH] FEAT: email validator --- .../formbuilderproject/MainActivity.kt | 4 +++- .../validator/EmailValidator.kt | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/validator/EmailValidator.kt diff --git a/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt b/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt index b425e40..290bda8 100644 --- a/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt +++ b/app/src/main/java/cz/qase/android/formbuilderproject/MainActivity.kt @@ -11,6 +11,7 @@ import cz.qase.android.formbuilderlibrary.element.* import cz.qase.android.formbuilderlibrary.element.generic.ActionCallback import cz.qase.android.formbuilderlibrary.element.generic.CheckboxCallback import cz.qase.android.formbuilderlibrary.element.generic.ValueCallback +import cz.qase.android.formbuilderlibrary.validator.EmailValidator import cz.qase.android.formbuilderlibrary.validator.MaxLengthFormValidator import cz.qase.android.formbuilderlibrary.validator.NotBlankFormValidator import cz.qase.android.formbuilderlibrary.validator.NotInPastValidator @@ -29,6 +30,7 @@ class MainActivity : AppCompatActivity() { private val longerStringValues = (1..5).map { randomString(25) } private val notEmptyValidator = NotBlankFormValidator("This field is empty") + private val emailValidator = EmailValidator("This is not valid email!") private val maxLengthValidator = MaxLengthFormValidator("This filed has more than 10 characters.", 10) private val validateActionCallback = object : ActionCallback, Form.Callback { @@ -125,7 +127,7 @@ class MainActivity : AppCompatActivity() { override fun callback(value: String) { Toast.makeText(applicationContext, value, Toast.LENGTH_SHORT).show() } - }, arrayListOf(notEmptyValidator))) + }, arrayListOf(emailValidator))) addSpace() //action elements diff --git a/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/validator/EmailValidator.kt b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/validator/EmailValidator.kt new file mode 100644 index 0000000..8bcdd0b --- /dev/null +++ b/formbuilderlibrary/src/main/java/cz/qase/android/formbuilderlibrary/validator/EmailValidator.kt @@ -0,0 +1,21 @@ +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 EmailValidator(private val errorMsg: String) : FormValidator { + override fun validate(value: String?) { + if (value.isNullOrBlank()) { + throw ValidationException(errorMsg) + } + if (!Patterns.EMAIL_ADDRESS.matcher(value).matches()) { + throw ValidationException(errorMsg) + } + } +}