Skip to content

funyin/FormValidator

Repository files navigation

FormValidator Android Arsenal Made in Nigeria

A form validation library for android jetpack compose

Features

  • Determine Validation Flow. Flow.Down|Flow.Up|Flow.Splash
  • Custom Validation
  • Validation Snackbar
  • Validation State Callbacks
  • Library is well documented

Preview

device-2022-01-15-014026.mp4

Example

@Composable
    fun ScreenContent() {
        var name by remember { mutableStateOf("") }
        var nameError by remember { mutableStateOf<String?>(null) }
        var email by remember { mutableStateOf("") }
        var emailError by remember { mutableStateOf<String?>(null) }
        val nameField = "Name"
        val emailField = "Email"
        val validator = FormValidator(
            fields = listOf(
                ValidationField(
                    value = name,
                    name = nameField,
                    onError = {
                        nameError = it
                    }),
                ValidationField(
                    value = email,
                    onError = {
                        emailError = it
                    }, type = FormValidator.Type.Email
                )
            )
        )
        Form(
            validator = validator,
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(state = rememberScrollState())
                .padding(16.dp)
        ) {
            Spacer(modifier = Modifier.height(30.dp))
            AppTextField(
                label = nameField,
                value = name,
                placeholder = "Enter name",
                onValueChanged = { name = it },
                errorMessage = nameError
            )
            Spacer(modifier = Modifier.height(16.dp))
            AppTextField(
                label = emailField,
                value = email,
                placeholder = "Enter email",
                onValueChanged = { email = it },
                errorMessage = emailError,
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email)
            )
            Spacer(modifier = Modifier.height(40.dp))
            AppButton(text = "Submit") {
                if(validator.validate())
                    showToast(validator.errorMessage)
            }
        }
    }

Getting started

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.funyin:FormValidator:1.0.0'
	}

BuyMeAShawrma.png