• Stars
    star
    170
  • Rank 216,274 (Top 5 %)
  • Language
    Kotlin
  • License
    MIT License
  • Created over 2 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A jetpack compose form builder implementation.

Jetpack Compose FormBuilder

A customisable android library used to provide an abstraction layer over form elements as well as provide a DRY code implementation of a form.

The library is used to help in the state management of a Form in Jetpack compose. Currently, we don't have an official support for a form state so the library is used to provide a custom implementation of the same.

Installation

In the root build.gradle file add the following:

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

Then add the following to your module's build.gradle

dependencies {
    implementation 'com.github.jkuatdsc:form-builder:${version}'
}

Basic Usage

The library provides a FormState class that represents the state of your Form. It receives a list of field classes that inherit the basic properties from the BaseState class.

val formState = FormState(
    fields = listOf(
        TextFieldState(
            name = "email",
            transform = { it.trim().lowercase() },
            validators = listOf(Validators.Email()),
        ),
        ChoiceState(
            name = "gender",
            validators = listOf(Validators.Required())
        ),
        SelectState(
            name = "hobbies",
            validators = listOf(Validators.Required())
        )
    )
)

You can pass a list of Validators to the field states. These validators are used to check the validity of the input in the specific field. The validators are executed in the order they are passed.

The transform function allows you to change the text field value to whatever class you desire, e.g, TextFieldState<String> to TextFieldState<Int>.

In your UI:

val formState = remember { viewmodel.formState }

val emailState: TextFieldState = formState.getState("email")

OutlinedTextField(
    value = emailState.text,
    isError = emailState.hasError,
    label = { Text("Email address") },
    onValueChange = { emailState.change(it) }
)
if (emailState.hasError) Text(emailState.errorMessage, color = MaterialTheme.colors.error)

We can get individual states for the fields using the getState function in the FormState class. We can then access various properties of the state like text, hasError, errorMessage etc.

Don't forget to update your state using the setter functions in each field state.

To validate your form and get form data:

if (formState.validate()) {
    val data = formState.getData(Credentials::class)
    Log.d("Data", "submit: data from the form $data")
}

Here is what the Credentials data class looks like. Take note of how the property names correspond to the field values passed when instantiating the form state.

data class Credentials(
    val email: String,
    val gender: String,
    val hobbies: List<String>
)

The validate function returns true if all the fields are valid. You can then access data from the form using the getData function. Pass in your data class and using reflection, we convert the map (Map<String, Any>) to your data class.

You can also pass in custom error messages to the validators.

TextFieldState(
    name = "age",
    transform = { it.toInt() },
    validators = listOf(Validators.MinValue(limit = 18, message = "too young"))
)

Demos

Demo 1 : In this demo, the validations are executed before the next screen is presented.

validate_screen.mp4

Demo 2 : In this demo, the validations are run at the end of the survey when submitting data.

validate_final.mp4

You can find the sample apk here

For more details about the library, check out the references.

Further Reading

The links below provide a reinforced understanding to the library.

MIT Licence

More Repositories

1

IoT

JKUAT DSC, Internet Of Things Track.Contains codes of Weekly DSC IOT Track meet-ups
C++
4
star
2

light-saver

Saving energy used by automation of lights to turn on only when a person is in a room
Shell
3
star
3

hashcode

Google hashcode problem statements
Python
3
star
4

android-study-2021

Content covered in DSC Android 2021.
Kotlin
3
star
5

dscjkuat-web

New website for GDSC JKUAT
Python
2
star
6

SimpleCalculator

A basic calculator
Kotlin
2
star
7

ktor-client-android

Kotlin
2
star
8

Python101

Some sample code to kickstart your python journey.
Python
2
star
9

Datascience101

Jupyter Notebook
2
star
10

MyLiving_App

MyLiving android application
Kotlin
2
star
11

RFID-ACCESS-MANAGEMENT

IOT example for RFID for access control and management
C++
1
star
12

discover_jkuat_portal_backend

Backend to Discover JKUAT students portal version 1.0.0
1
star
13

dsc-mailer

A simple mailing library for python powered applications using Sendgrid.
Python
1
star
14

Desk-Assistant

Weather station that links to your google calendar and fetches all your tasks to show on Oled display.
C
1
star
15

RedditClone

A simple reddit clone
1
star
16

blockchian_and_web3

The journey into blockchain and web3 technologies
Solidity
1
star
17

JkuatApplication

Introducing our cutting-edge school internal application—a dynamic web app designed to revolutionize the way students and faculty engage with academic life. Packed with essential features, our application boasts a vibrant main page featuring real-time feeds, ensuring everyone stays connected with the latest updates and announcements.
Python
1
star
18

Data-Science-Curriculumn-2022-Project-Based

Jupyter Notebook
1
star
19

Web101

1
star