A jetpack compose form builder implementation.

Overview

Jetpack Compose FormBuilder

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

Introduction

The library aims to provide a dynamic way to build a Form using Jetpack compose input fields. There are common actions that are performed with forms such as validation and management of data.

With the current version of Jetpack compose, we don't have a Form composable which leads to a lot of redundant code that can get messy really quick. Assume you have 10 text fields, you have to manage the state and validation of each field individually. This will lead to a lot of variables and if/when expressions in that single composable.

Features

The library approaches the problem as follows:

1. Form composable

We have a Form composable that is in charge of drawing all the input fields on the screen. It has its own state used to validate the fields it contains and returns the data from the form. The data returned from the form is in the form of Map where the fields' names are the keys in the map.

2. Validators

This is a group of methods that perform an actual check on the field's data. They are used to check if the data in the field matches the specified criteria. Available validators are:

  • Required: this requires that a field should not be empty
  • Email: this requires the field value to match the Email regex
  • Min: this receives an argument which the value should be greater than
  • Max: this receives an argument which the value should not exceed or should be less than

3. Input fields

There should be a field composable that accepts the following attributes:

  • name: this will be used as the key in the form's result/data
  • validators: this is a list containing all possible validators to be performed on that field
  • styles: theses are a couple of attributes available on the normal Material TextField

Once you call validate from the Form's state, all the form fields must be validated and the method should return a boolean to denote whether all the validators have passed or any has failed. The form state should also have a method to access all the data from each of the fields in the Form.

Usage

A simple form element should have one state. It should receive a list of fields each with their own attributes.

@Composable
fun MyForm(){
    val formState by remember { FormBuilderState() }
    
    FormBuilder(
        state = state,
        fields = [
            FormField(
                name = "email",
                validators = [Required, Email]
            ),
            FormField(
                name = "age",
                validators = [Required, Min(18)]
            )
        ]
    )
    
    // to validate
    if (state.validate()){
        val data = state.values() // {[email protected], age=13}
    }
}

MIT Licence

Comments
  • How do I prepopulate with existing  data

    How do I prepopulate with existing data

    If I have an update form that I wish to prepopulate with data which method should be called. Or is there a need to create the setData equivalent of getData on FormState?

    opened by andre-artus 6
  • Validators

    Validators

    Just as the README states, we will need various functions specified in the library for validations.

    You can start by creating an open class to define your validators:

    sealed interface Validators
    open class Email(var message: String): Validators
    open class Required(var message: String): Validators
    open class Min(var message: String, var limit: Double): Validators
    open class Max(var message: String, var limit: Double): Validators
    open class Regex(var message: String, var regex: String): Validators
    

    Then in the state class, you can add the actual implementation of the validator:

    private fun required(): Boolean = text.isNotEmpty()
    

    In case of any question, you can reach out to me or check this repo for further guidance.

    opened by LinusMuema 3
  • Add validation for dropdown menu

    Add validation for dropdown menu

    This library is awesome, can we validate dropdown menu (spinner) with form_builder? If it is possible than show us how to implement it or if it is not possible than please add this validation too. thnx.

    opened by salauddin23shumon 2
  • More validations

    More validations

    Here's a list of more validations that we can add to our already existing validations list.

    1. Phone: this can be used to verify phone numbers. From the android util patterns, we can verify this data just like we do with emails.
    2. Web URL: this can be used to verify links. Simply that. Same case as the phone, we have android utils.
    3. Cards: We can use this to verify credit card numbers.
    enhancement 
    opened by LinusMuema 0
  • Feature nav buttons

    Feature nav buttons

    Added a new navigation composable that has a button. It receives two arguments:

    • screen: this is the current page in the survey flow
    • navigate: the lambda function to be executed once the user clicks the button.

    In the SurveyViewmodel we will be calling navigate function to change the screen once the user clicks the button in this composable. We will also need to check if the Validate on each screen option is set to true. If so, we will validate the current screen before changing the page.

    This fixes #32

    opened by LinusMuema 0
  • Feature tab layout

    Feature tab layout

    Implemented the tab layout composables. Since there is no Material composable for such a behaviour, I implemented a custom tab using the Canvas API.

    Using Modifier.onGloballyPositioned I am able to get the center of each text header in the Tab. I use this as the offset on the X-axis to draw the indicators below the Text using the Canvas.drawCircle function. I also use the same to draw the line that connects the indicators.

    Animations use the simple AnimatedVisibility composable.

    This solves #31

    opened by LinusMuema 0
  • Navigation

    Navigation

    This involves the buttons on each of the survey pages.

    TO-DO's

    • [x] Add the buttons composable
    • [x] Implement navigation between the survey pages
    • [x] Start validations if needed
    • [x] Navigate to ExitActivity if needed
    enhancement 
    opened by LinusMuema 0
  • Tab layout

    Tab layout

    This includes the indicator at the top of the screen.

    TO-DO's

    • [x] Create the TabLayout composable
    • [x] Implement the state changes and update the UI on navigation

    Nice to have's

    • [x] Animate the visibility on navigation
    • [x] Animate the transition on navigation
    enhancement 
    opened by LinusMuema 0
  • Survey validation

    Survey validation

    TO-DOs

    • [x] Implement the validate on each screen flow
    • [x] Add the individual form validations
    • [x] Implement the whole survey validation
    • [x] Navigate to the error screens if needed

    Nice to have's

    • [ ] Animate the navigation to the error screens
    enhancement 
    opened by LinusMuema 0
  • Survey state management

    Survey state management

    In the SurveyViewmodel class, create a the form states to hold the states of all the fields of a single page the survey. All fields must have the Validators.Required validator.

    TO-DO's

    • [x] Create the different form states
    • [x] Add field states to the specific form states
    • [x] Add the respective validators in each of the field states
    enhancement 
    opened by LinusMuema 0
Releases(1.0.5)
  • 1.0.5(Oct 20, 2022)

    What's Changed

    Feature/additional validators by @SergeiMikhailovskii in https://github.com/jkuatdsc/form-builder/pull/47. Three validators added to the TextFieldState

    • Phone: this can be used to verify phone numbers.
    • Web URL: this can be used to verify links.
    • Cards: this can be used to verify credit card numbers.

    New Contributors

    • @SergeiMikhailovskii made their first contribution in https://github.com/jkuatdsc/form-builder/pull/47

    Full Changelog: https://github.com/jkuatdsc/form-builder/compare/1.0.4...1.0.5

    Source code(tar.gz)
    Source code(zip)
  • 1.0.3(Sep 8, 2022)

  • v1.0.2(Jul 17, 2022)

    What's Changed

    • Added initial: <T> argument in BaseState class
    • Changed the access modifier for value and fun validate() in BaseState class to internal
    Source code(tar.gz)
    Source code(zip)
  • 1.0.1(Apr 3, 2022)

  • 1.0.0(Feb 17, 2022)

Owner
DSC-JKUAT
DSC-JKUAT
Luis David Orellana 3 Jun 20, 2022
Compose-Ratingbar-library - A simple implementation for rating bar in Jetpack Compose

Compose-Ratingbar-library - A simple implementation for rating bar in Jetpack Compose

Mahmoud Hussein 14 Dec 21, 2022
A simple implementation of collapsing toolbar for Jetpack Compose

compose-collapsing-toolbar A simple implementation of CollapsingToolbarLayout for Jetpack Compose Installation You should add mavenCentral() repositor

onebone 321 Dec 31, 2022
An implementation of the Bloc pattern for Kotlin and Jetpack Compose

Kotlin Bloc An implementation of the Bloc pattern for Kotlin and Jetpack Compose. Documentation Documentation is available here: https://ptrbrynt.gith

Peter Bryant 21 Dec 7, 2022
Jetpack Compose implementation of Discord's Overlapping Panels

OverlappingPanelsCompose Jetpack Compose implementation of Discord's Overlapping Panels Installation Groovy Add JitPack repository to root build.gradl

Xinto 9 Jul 11, 2022
ComposeImageBlurhash is a Jetpack Compose component with the necessary implementation to display a blurred image while the real image is loaded from the internet. Use blurhash and coil to ensure good performance.

compose-image-blurhash ComposeImageBlurhash is a Jetpack Compose component with the necessary implementation to display a blurred image while the real

Orlando Novas Rodriguez 24 Nov 18, 2022
Luis David Orellana 11 Jan 1, 2023
A Jetpack Compose implementation of Owl, a Material Design study

Owl sample This sample is a Jetpack Compose implementation of Owl, a Material De

Alexander 1 Feb 26, 2022
Android Jetpack Compose implementation of SpinKit with additionals

ComposeLoading Android Jetpack Compose implementation of SpinKit with additionals. How it looks Preview Setup Open the file settings.gradle (it looks

Emir Demirli 8 Dec 18, 2022
Jetpack Compose Boids | Flocking Insect 🐜. bird or Fish simulation using Jetpack Compose Desktop 🚀, using Canvas API 🎨

?? ?? ?? Compose flocking Ants(boids) ?? ?? ?? Jetpack compose Boids | Flocking Insect. bird or Fish simulation using Jetpack Compose Desktop ?? , usi

Chetan Gupta 38 Sep 25, 2022
A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Why Not Compose! A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Md. Mahmudul Hasan Shohag 186 Jan 1, 2023
Learn Jetpack Compose for Android by Examples. Learn how to use Jetpack Compose for Android App Development. Android’s modern toolkit for building native UI.

Learn Jetpack Compose for Android by Examples. Learn how to use Jetpack Compose for Android App Development. Android’s modern toolkit for building native UI.

MindOrks 382 Jan 5, 2023
This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Compose.

JetBMICalculator This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Co

BHAVNA THACKER 3 Dec 31, 2022
Jetpack-Compose-Demo - Instagram Profile UI using Jetpack Compose

Jetpack-Compose-Demo Instagram Profile UI using Jetpack Compose

omar 1 Aug 11, 2022
Jetpack-compose-animations-examples - Cool animations implemented with Jetpack compose

Jetpack-compose-animations-examples This repository consists of 4 animations: St

Canopas Software 180 Jan 2, 2023
Compose-navigation - Set of utils to help with integrating Jetpack Compose and Jetpack's Navigation

Jetpack Compose Navigation Set of utils to help with integrating Jetpack Compose

Adam Kobus 5 Apr 5, 2022
Jetpack-compose-uis - A collection of some UIs using Jetpack Compose. built using Katalog

Jetpack Compose UIs This is a collection of some UIs using Jetpack Compose. It i

Mori Atsushi 3 Dec 15, 2022
A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose

Authentication A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose Scree

Felix Kariuki 5 Dec 29, 2022
An application that i developed with a aim of learning Jetpack compose and many other jetpack libraries

An application that i developed with a aim of learning Jetpack compose and many other jetpack libraries, The application make use of jikan Api which displays a list of animations,there more details and even trailers of the animations.

Odhiambo Brandy 10 Nov 23, 2022