there are lots of boring ways to check form validation!
this library offers an easy way to validate form for android apps.
the library will work with
TextViewsEditTextsCheckBoxsCollectionStringIntFloatDoubleDateAdditional Targetcheck
- Step 1. Add the JitPack repository to your build file.
Add it in your root build.gradle at the end of repositories.
allprojects {
repositories {
..
maven { url 'https://jitpack.io' }
}
}- Step 2. Add the dependency
dependencies {
implementation 'com.github.hsnmrd:RaikaFormValidation:$version'
}- Step 3. use
RaikaFormValidationclass andaddConstraint,isValidatefunctions.
RaikaFormValidation()
.addConstraint(etFirstName) {
isRequire {
// todo : control error
}
}
.addConstraint(etEmail) {
isEmail {
// todo : control error
}
isRequire {
// todo : control error
}
}
.isValidate {
// form is valid
}fun <T> addConstraint(
target: T,
type: T.() -> Unit,
): RaikaFormValidation {}targetpass the target which is going to have a limit.typesome restrictions are available due to the target passed.
-
EditText,TextViewisRequire {}isEmail {}isLengthAtMost {}isLengthLessThan {}isLengthGreaterThan {}isLengthIn {}isLengthEqual {}isContaining {}isEqual {}isContaining {}isContainingNumber {}isContainingUpperCase {} -
StringisNotNull {}isRequire {}isEmail {}isLengthAtMost {}isLengthLessThan {}isLengthGreaterThan {}isLengthIn {}isLengthEqual {}isContaining {}isEqual {}isContainingNumber {}isContainingUpperCase {} -
CollectionisRequire {}isSizeAtMost {}isSizeLessThan {}isSizeAtLeast {}isSizeGreaterThan {}isSizeIn {}isSizeEqual {}isNotNull {} -
CheckBoxisChecked {} -
IntFloatDoubleDateisNotNull {}isAtMost {}isLessThan {}isAtLeast {}isGreaterThan {}isIn {}isEqual {}
fun isValidate(listener: () -> Unit) {}if there is a type which is not supported yet, here is a way to implement your custom restriction.
- make your custom restriction by using
checkConstraintResult ()function and pass aconditionas an argument.
checkConstraintResult's callback will call if the passed argument (condition) is false.
so improve your restriction by making a lambda which is callederrorListeneras shown below and call it when the condition result is false.
fun Type.yourRestrictionName(errorListener: () -> Unit) {
checkConstraintResult(condition) { errorListener?.invoke() }
} for more explanation check one of written restriction.
fun TextView.isRequire(errorListener: () -> Unit) {
checkConstraintResult(this.text.toString().trim().isNotEmpty()) { errorListener?.invoke() }
}Type : TextView
yourRestrictionName : isRequire
condition : this.text.toString().trim().isNotEmpty()
- the error handler is optional now
RaikaFormValidation()
.addConstraint(etFirstName) {
isRequire()
}isEmailstatus is checked when the target is not empty (uses inEditText,TextViewandString)
- some bugs fixed
- library introduced