Skip to content

Commit

Permalink
FEAT: email validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Zdeněk Balák committed May 3, 2019
1 parent 21abcb8 commit 761b10c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> {
override fun validate(value: String?) {
if (value.isNullOrBlank()) {
throw ValidationException(errorMsg)
}
if (!Patterns.EMAIL_ADDRESS.matcher(value).matches()) {
throw ValidationException(errorMsg)
}
}
}

0 comments on commit 761b10c

Please sign in to comment.