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
TextViews
EditTexts
CheckBoxs
Collection
String
Int
Float
Double
Date
Additional Target
check
- 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
RaikaFormValidation
class andaddConstraint
,isValidate
functions.
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 {}
target
pass the target which is going to have a limit.type
some restrictions are available due to the target passed.
-
EditText
,TextView
isRequire {}
isEmail {}
isLengthAtMost {}
isLengthLessThan {}
isLengthGreaterThan {}
isLengthIn {}
isLengthEqual {}
isContaining {}
isEqual {}
isContaining {}
isContainingNumber {}
isContainingUpperCase {}
-
String
isNotNull {}
isRequire {}
isEmail {}
isLengthAtMost {}
isLengthLessThan {}
isLengthGreaterThan {}
isLengthIn {}
isLengthEqual {}
isContaining {}
isEqual {}
isContainingNumber {}
isContainingUpperCase {}
-
Collection
isRequire {}
isSizeAtMost {}
isSizeLessThan {}
isSizeAtLeast {}
isSizeGreaterThan {}
isSizeIn {}
isSizeEqual {}
isNotNull {}
-
CheckBox
isChecked {}
-
Int
Float
Double
Date
isNotNull {}
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 acondition
as an argument.
checkConstraintResult
's callback will call if the passed argument (condition
) is false.
so improve your restriction by making a lambda which is callederrorListener
as 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()
}
isEmail
status is checked when the target is not empty (uses inEditText
,TextView
andString
)
- some bugs fixed
- library introduced