Simplify Form Validation using Kotlin Flow on Android

Ashish Suthar
3 min readNov 6, 2022
Form Validation Simplified for Android

Form validation is the most tedious task. We all struggled with that while working with different kinds of applications. Validating data is most important for maintaining the integrity of user inputs.

Recently, I was working on an app, that has many forms with different types of fields. So I wanted to find a simple solution for validating forms in a pragmatic and scalable way with clean code. With the help of Kotlin Flow and FlowBinding somehow I managed to implement clean code for Form Validations.

Let’s begin the implementation. The technique given in this article can work on almost any kind of form field. I will use the form with most general fields like Username, Email, Phone Number, Password, and Confirm Password for demonstration.

Requirements

I assume your project already uses Material Components (TextInputLayout and TextInputEditText), View Binding, and lifecycleScope.

Other requirements are

  • Kotlin Coroutines — for using StateFlow.
  • FlowBinding — It offers an extensive set of extension functions that turn traditional callbacks/listeners on Android UI widgets into the Flow type.

Implementation

After analyzing the forms, I have figured out their most basic needs. Any form field can have its states and some methods like validate, clearError, clearFocus, enable and disable. The form field state will hold the current value and the isValid state will help us to determine whether the FormField value is valid or not.

Here is the base class of all types of form fields.

So basically, In our app, we will create Form Fields and perform validation on each field. Each field will have its own TextInputLayout, TextInputEditText, and some validation rules.

To represent the above logic. We need a class! We will inherit the above FormField into our FormFieldText class. FormFieldText will be used for representing text fields in our forms.

Also, other types of form fields can be implemented using FormField class, such as FormFieldDate, FormFieldTime, etc. but We will discuss them later. Or reach out to me directly.

Implementation of FormFieldText

Now we can use FormFieldText class for declaring fields in our activity or fragment.

For example, there is a Username field in the form. Username is a required field. We can declare it like the below code.

The username field is placed in the form in activity_main.xml

Up to here, We have implemented:

  1. FormField — Base class for a different type of form fields
  2. FormFieldText — FormField implementation for text type fields
  3. Username field — For learning how to declare a field in activity (or fragments) with validation rules using FormFieldText class

In the above code, We haven't considered many fields in the form. The technique given in this article is the most useful for validating small to large forms.

Let me ask you one question! What is form?

You will answer: “Form is a collection of fields” 100% correct answer!

In the next part of this article, we will see the practical use of the above implementation with multiple fields.

Thanks for reading!

--

--