Skip to content
A declarative Form Validation for Android, simple, clean, and customizable.
Kotlin Java
Branch: master
Clone or download
Latest commit 3e0d6e4 Jan 22, 2020
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
blob/master/raw add diagram & remove redundant files Dec 23, 2019
buildSrc fix sample build issue Jan 4, 2020
core fix sample build issue Jan 4, 2020
gradle/wrapper update Grdle Dec 17, 2019
rxjava
sample fix sample build issue Jan 4, 2020
.gitignore remove ignored files Dec 24, 2019
CHANGELOG.md Update CHANGELOG.md Dec 23, 2019
README.md Update README.md Jan 22, 2020
TEXTVIEW.md Update TEXTVIEW.md Dec 23, 2019
build.gradle improve code Dec 24, 2019
common-android-library.gradle remove redundant dependencies & add test dependencies Dec 14, 2019
common.gradle use kotlin extensions and common Gradle in dependency managemnt Dec 17, 2019
gradle.properties first commit May 13, 2019
gradlew First commit. Nov 11, 2017
gradlew.bat ignore sample/build Dec 11, 2019
settings.gradle separate RxJava in a different modeul Dec 17, 2019

README.md

FormValidator for Android

A declarative Form Validation for Android, simple, clean, and customizable.

Every time you create a form, you need to declare fields and write code for for validating each field in the form, and this results in many if else and a lot of boilerplate. For these reasons FormValidator is here, just declare your fields in XML and its validation and all things will be done for you!

<com.sha.formvalidator.Form ... >
    <com.sha.formvalidator.widget.FormEditText 
    	app:validationType="email"
    ... />
    
    <com.sha.formvalidator.widget.FormCheckBox 
    	app:checkBoxValidation="checked"
    ... />
    
    <com.sha.formvalidator.widget.FormToggleButton 
    	app:toggleButtonValidation="on"
    ... />
    
    <com.sha.formvalidator.widget.FormSwitch 
    	app:switchValidation="on"
    ... />
</com.sha.formvalidator.Form>

// validate
val isFormValid = findViewById<Form>(R.id.form).validate()

Installation

Gradle:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

dependencies {
        // Core
        implementation 'com.github.ShabanKamell.FormValidator:core:x.y.z'
        // RxJava
        implementation 'com.github.ShabanKamell:FormValidator:x.y.z'
}

(Please replace x, y and z with the latest version numbers: )

Widgets

FormValidator has a collection of different widgets that implement Validatable. There're TextView widgets and other widgets.

TextView Widgets

Widget Required attributes Default
FormEditText see TextView Validation -
FormAutoCompleteTextView see TextView Validation -

Other Widgets

Widget Required attributes Default
FormCheckBox checkBoxValidation checked
FormRatingBar ratingBarValidation required
FormSeekBar seekBarValidation required
FormSwitch switchValidation on
FormToggleButton toggleButtonValidation on

Validatable interface

Validatable is a fubctional interface implemented by widgets to support validation.

interface Validatable {
    // return true if valid, false otherwise.
    fun validate(): Boolean
}

Form Layout

Form is a LinearLayout that warps all widgets and provides APIs for triggering validation with options.

Declare Form in XML

<com.sha.formvalidator.Form
        ..
        android:id="@+id/form"
        app:shakeOnError="true"
        app:ignoreHiddenFields="true"
        >
    <com.sha.formvalidator.widget.FormEditText 
    	app:validationType="email"
    ... />
    
    <com.sha.formvalidator.widget.FormCheckBox 
    	app:checkBoxValidation="checked"
    ... />
    
    <com.sha.formvalidator.widget.FormToggleButton 
    	app:toggleButtonValidation="on"
    ... />
    
    <com.sha.formvalidator.widget.FormSwitch 
    	app:switchValidation="on"
    ... />
</com.sha.formvalidator.Form>
   

Set options programmatically

form.options = FormOptions.create {
       validationInterceptor = { .. }
       ignoreFieldsIds = listOf(R.id.etIgnoredId)
       ignoreHiddenFields = true
       shakeOnError = true
}

Trigger Validation

val isValid = form.validate()

// OR
form.validateOnClick(btnValidateForm) { isValid -> ..}

TextView Validation

FormValidator contains rich validators for validating TextView. There're are 2 predefined widgets that inherit from TextView: FormEditText and FormAutoCompleteTextView. see full documentation in 👉 👉 TextView Validation

See 'sample' module for the full code.

Credit

Android Form EditText.

🛡 License

click to reveal License
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You can’t perform that action at this time.