A lightweight, simplified form validation library for Android

Screenshots

Validation Rules Setup

<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:app="http://schemas.android.com/apk/res-auto">

    <formField
        app:id="@id/inputUsername"
        app:required="true"
        app:minSize="3"
        app:maxSize="25" />

    <formField
        app:id="@id/inputEmail"
        app:email="true" />

    <formField
        app:id="@id/inputPhone"
        app:required="true"
        app:phoneNumber="true" />

    <formField
        app:id="@id/inputPassword"
        app:required="true"
        app:minSize="3"
        app:maxSize="25" />

</resources>

Form Layout Setup

<me.ibrahimsn.lib.FreyaForm
    android:id="@+id/freya"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:config="@xml/form_app">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/inputEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12dp">

        <com.google.android.material.textfield.TextInputEditText
            android:inputType="textEmailAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Email Address"/>

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/inputPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true"
        android:layout_marginBottom="16dp">

        <com.google.android.material.textfield.TextInputEditText
            android:inputType="textPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"/>

    </com.google.android.material.textfield.TextInputLayout>

</me.ibrahimsn.lib.FreyaForm>

Validation on Form Submit

val freya = findViewById<FreyaForm>(R.id.freya)
val submitButton = findViewById<Button>(R.id.submit)

/*
 * Listens if the whole form is valid
 */
freya.onValidationChangeListener = {
    Log.d(TAG, "Is form valid: $it")
}

/*
 * Listens for form validation errors after validate()
 */
freya.onErrorListener = {
    Log.d(TAG, "Validation errors: $it")
}

submitButton.setOnClickListener {
    Log.d(TAG, "Is form valid: ${freya.validate()} values: ${freya.values}")
}

Realtime Field Validation

/*
 * Listens for validation changes of any form field
 */
freya.onFieldValidationChangeListener = {
    when (it.error) {
        is Ruler.Required -> {
            it.setError("This field is required.")
        }
        is Ruler.Email -> {
            it.setError("Please enter a valid email address.")
        }
        is Ruler.PhoneNumber -> {
            it.setError("Please enter a valid phone number.")
        }
        is Ruler.MinSize -> {
            it.setError("This field must contain at least ${it.error?.param} characters.")
        }
        is Ruler.MaxSize -> {
            it.setError("This field must contain at most ${it.error?.param} characters.")
        }
        is Ruler.Regex -> {
            it.setError("Please provide a valid data.")
        }
    }
}

Get Values & Prefill Form

val values: Map<Int, Any?> = freya.values

freya.setup(
    mapOf(
        R.id.inputUsername to "Thomas"
    )
)

Note

Currently supported field view types are TextInputLayout, TextInputEditText, EditText.
Support for more view types and new validation rules will be added in the future.

Download Demo APK

Setup

Follow me on Twitter @ibrahimsn98

Step 1. Add the JitPack repository to your build file

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

Step 2. Add the dependency

dependencies {
    implementation 'com.github.ibrahimsn98:freya:1.0.0'
}

GitHub

https://github.com/ibrahimsn98/Freya