formz-kotlin
1.0.1indexedSimplifies form representation and validation with an implementation inspired by the Formz Dart library, enhancing usability and efficiency in handling form data.
Simplifies form representation and validation with an implementation inspired by the Formz Dart library, enhancing usability and efficiency in handling form data.
Formz-Kotlin is a Kotlin Multiplatform library for form representation and validation, inspired by the Dart Formz library. It provides a structured and reusable way to manage form state and validation in your Kotlin projects.
Formz-Kotlin addresses the following challenges:
To use Formz-Kotlin in your project, add the following dependency to your build.gradle.kts file:
implementation("io.github.zainulhassan815:formz:1.0.1")
Let's walk through creating a simple login form with email and password fields.
First, define the validation errors and the FormInput classes for email and password.
enum class EmailError {
Empty
}
class Email(value: String) : CachedFormInput<String, EmailError>(value) {
override fun validator(value: String): EmailError? = when {
value.isBlank() -> EmailError.Empty
else -> null
}
}
Password
enum class PasswordError {
Empty,
TooShort
}
class Password(value: String) : FormInput<String, PasswordError>(value) {
override fun validator(value: String): PasswordError? = when {
value.isBlank() -> PasswordError.Empty
value.length < 8 -> PasswordError.TooShort
else -> null
}
}
Next, create a data class that represents the state of your login form. This class should implement the Form interface.
data class LoginFormState(
val email: Email = Email(""),
val password: Password = Password(""),
val submissionStatus: FormSubmissionStatus = FormSubmissionStatus.Initial
) : Form {
override val inputs = listOf(email, password)
}
Now, you can use the LoginFormState in your ViewModel to manage the form's state.
class ViewModel {
private val _state = MutableStateFlow(LoginFormState())
state: StateFlow<LoginFormState> = _state
{
viewModelScope.launch {
_state.update { it.copy(password = Password(password, )) }
}
}
{
form = _state.value
(form.isValid) {
} {
}
}
}
Contributions are welcome! If you have any ideas, suggestions, or issues, please open an issue or create a pull request on the GitHub repository.
Formz-Kotlin is licensed under the Apache License 2.0.
Surfaced from shared tags and platforms — no rankings paid for.