rememberField

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

Creates and remembers a form field control with validation and state management.

This function creates a FormField control that can be used to manage field state and interactions. Unlike the Field composable functions, this returns the control object directly without rendering UI, allowing for more flexible usage patterns.

Usage:

val emailField = form.rememberField(
selector = { it.email },
updater = { copy(email = it) },
validator = FieldValidator {
notBlank { "Email is required" }
email { "Must be a valid email" }
}
)

TextField(
value = emailField.value,
onValueChange = emailField::onValueChange,
isError = emailField.hasError
)

Return

A FormField control object for managing the field state and interactions.

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.


fun <T, V, S, U> Form<T>.rememberField(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): FormField<U>

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

This function creates a FormField control with type adaptation capabilities, allowing you to convert between the stored value type (V), validation target type (S), and input type (U). Unlike the Field composable functions, this returns the control object directly without rendering UI, providing maximum flexibility for custom field implementations.

Usage:

val ageField = form.rememberField(
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" }
}
)

TextField(
value = ageField.value, // String type from adapter
onValueChange = ageField::onValueChange,
isError = ageField.hasError
)

Return

A FormField control object for managing the field state and interactions.

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.