Skip to content

A simple library to cleanly validate on-screen input views. You can update your destination view(for ex: enable/disable button to continue) in the result of validation.

Notifications You must be signed in to change notification settings

asimanaghayev/Validator

Repository files navigation

Validator

A simple library to cleanly validate on-screen input views. You can update your destination view(for ex: enable/disable button to continue) in the result of validation.

Demo

Setup

Gradle
repositories {
  mavenCentral()
}

dependencies {
  implementation 'com.github.asimanaghayev:Validator:v0.1.1'
}

Usage

Input Field

You have implement Validatable interface in your input field. 2 methods will be implemented in this view

Validity State of View

/**
 * Input field is valid if text is entered
 * */
override val isValid: Boolean
    get() = length() > 0

Informing Validator about change in input field

    override fun setupValidationListener(validator: Validator?) {
        addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            /**
             * To inform Validator view is changed
             * */
            override fun afterTextChanged(p0: Editable?) {
                validator?.checkAll()
            }
        })
    }

Output View

For output field you have to implement Verifier interface.

Updating Output on validity change

/**
 *  Enabling button if validator informs that all inputs are valid
 */
override fun onValid(isValid: Boolean) {
    isEnabled = isValid
} 

Binding Verifier and Validatable views

You have add this line to your activity/fragment for validation binding First variable is your Verifier view then you can add all of your Validatable views, there is no Validatable view limit

Binding

ValidationBinder.bind(binding.btnContinue, binding.inputFirst, binding.inputSecond)

About

A simple library to cleanly validate on-screen input views. You can update your destination view(for ex: enable/disable button to continue) in the result of validation.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages