Skip to content

hsnmrd/RaikaFormValidation

Repository files navigation

RaikaFormValidation

API Language

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

Contents

Usage

  • 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 and addConstraint, isValidate functions.
RaikaFormValidation()
	.addConstraint(etFirstName) {
	    isRequire {
		// todo : control error
	    }
	}
	.addConstraint(etEmail) {
	    isEmail {
		// todo : control error
	    }
	    isRequire {
		// todo : control error
	    }
	}
	.isValidate {
		// form is valid
	}

Functions

1. addConstraint: add the Restriction to specific target

fun <T> addConstraint(
	target: T,
	type: T.() -> Unit,
): RaikaFormValidation {}

Params

  • target pass the target which is going to have a limit.
  • type some restrictions are available due to the target passed.

Restrictions

  • 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 {}

2. isValidate: check if the form is valid

fun isValidate(listener: () -> Unit) {}

Supporting Additional Target

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 a condition 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 called errorListener 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()

Change Log

version 1.0-alpha4

  • the error handler is optional now
RaikaFormValidation()
	.addConstraint(etFirstName) {
	    isRequire()
	}
  • isEmail status is checked when the target is not empty (uses in EditText, TextView and String)

version 1.0-alpha2

  • some bugs fixed

version 1.0-alpha1

  • library introduced