rememberField

fun <T> Form<T>.rememberField(selector: (T) -> TextFieldState, validator: FieldValidator<CharSequence>? = null, name: FieldName? = null, dependsOn: FieldNames? = null, enabled: Boolean = true): FormTextField(source)

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

This function creates a FormTextField control that can be used to manage text 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.emailState },
validator = FieldValidator {
notBlank { "Email is required" }
email { "Must be a valid email" }
}
)

TextField(
state = emailField.state,
isError = field.hasError,
modifier = Modifier.onFocusChanged { state ->
emailField.handleFocus(state.isFocused)
}
)

Return

A FormTextField control object for managing the text field state and interactions.

Parameters

T

The type of the form data.

selector

A function that extracts the TextFieldState from the form data.

validator

Optional validator for TextFieldState.text.

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, S> Form<T>.rememberField(selector: (T) -> TextFieldState, adapter: TextFieldStateAdapter<S>, validator: FieldValidator<S>? = null, name: FieldName? = null, dependsOn: FieldNames? = null, enabled: Boolean = true): FormTextField(source)

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

This function creates a FormTextField control with type adaptation capabilities, allowing you to convert the text content to a different type for validation. Unlike the Field composable functions, this returns the control object directly without rendering UI, providing maximum flexibility for custom text field implementations.

Usage:

val ageField = form.rememberField(
selector = { it.ageState },
adapter = IntTextFieldStateAdapter(),
validator = FieldValidator<Int> {
min(0) { "Age must be non-negative" }
max(150) { "Age must be realistic" }
}
)

Column {
TextField(
state = ageField.state,
isError = field.hasError,
modifier = Modifier.onFocusChanged { state ->
ageField.handleFocus(state.isFocused)
}
)
}

Return

A FormTextField control object for managing the text field state and interactions.

Parameters

T

The type of the form data.

S

The type used for validation after adaptation.

selector

A function that extracts the TextFieldState from the form data.

adapter

The adapter that converts text content to the validation type.

validator

Optional validator for the adapted 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.