Field

fun <T, V> Form<T>.Field(selector: (T) -> V, updater: T.(V) -> T, validator: FieldValidator<V>? = null, name: FieldName? = null, dependsOn: FieldNames? = null, enabled: Boolean = true, render: @Composable (FormField<V>) -> Unit)

Creates a form field with validation and state management.

This function creates a form field that automatically handles value changes, validation, and state management. The field integrates with the parent form and participates in form-wide validation.

Usage:

form.Field(
selector = { it.email },
updater = { copy(email = it) },
validator = FieldValidator {
notBlank { "Email is required" }
email { "Must be a valid email" }
},
render = { field ->
TextField(
value = field.value, // String type
onValueChange = field::onValueChange,
isError = field.hasError
)
}
)

Parameters

T

The type of the form data.

V

The type of the field value.

selector

A function that extracts the field value from the form data.

updater

A function that updates the form data with a new field value.

validator

Optional validator for the field value.

name

Optional custom name for the field. If null, an auto-generated name is used.

dependsOn

Optional list of field names this field depends on for validation.

enabled

Whether the field is enabled for input.

render

The composable content that renders the field UI.


fun <T, V, S, U> Form<T>.Field(selector: (T) -> V, updater: T.(V) -> T, adapter: FieldTypeAdapter<V, S, U>, validator: FieldValidator<S>? = null, name: FieldName? = null, dependsOn: FieldNames? = null, enabled: Boolean = true, render: @Composable (FormField<U>) -> Unit)

Creates a form field with type adaptation, validation, and state management.

This overload allows you to use a type adapter to convert between the stored value type (V), validation target type (S), and input type (U). This is useful when you need to validate or display a field value in a different format than how it's stored.

Usage:

form.Field(
selector = { it.age },
updater = { copy(age = it) },
adapter = IntStringAdapter(),
validator = FieldValidator<Int> {
min(0) { "Age must be non-negative" }
max(150) { "Age must be realistic" }
},
render = { field ->
TextField(
value = field.value, // String type
onValueChange = field::onValueChange
)
}
)

Parameters

T

The type of the form data.

V

The type of the field value as stored in the form data.

S

The type used for validation.

U

The type used for input/display.

selector

A function that extracts the field value from the form data.

updater

A function that updates the form data with a new field value.

adapter

The type adapter that converts between V, S, and U types.

validator

Optional validator for the validation target type (S).

name

Optional custom name for the field. If null, an auto-generated name is used.

dependsOn

Optional list of field names this field depends on for validation.

enabled

Whether the field is enabled for input.

render

The composable content that renders the field UI.