FormField

class FormField<T>(val required: Boolean = true, initialValue: T? = null) : MutableStateFlow<T?>

A FormField is a MutableStateFlow, so it can be collected and its value updated like any other MutableStateFlow.

But it also has parameters that make it suitable for use in a form, such as validators, error stateflow, feedback stateflow, and focus request.

Form field validators are run in the order they are added to the form field. If a validator fails, the remaining validators are not run. If you make use of the feedback property, it might be best practice to add only one validator.

Parameters

required

Whether this form field is optional or not. If true, the form field value is required to be non-null or non-empty to be validated. If false, the field value is optional and will be valid if it is empty or null.

Note that if required is false and the value of this form field is neither null nor empty, the field will be validated as if it were required.

initialValue

The initial value of this form field. Would typically be null or an empty string unless the form field is pre-filled (no pun intended).

Constructors

Link copied to clipboard
constructor(required: Boolean = true, initialValue: T? = null)

Properties

Link copied to clipboard
val error: StateFlow<String?>
Link copied to clipboard

If this is false, the form field's error state flow value will always be null.

Link copied to clipboard
val feedback: StateFlow<Any?>
Link copied to clipboard
val focusRequest: SharedFlow<Unit>

This emits a Unit value when the field is to be brought to the user's attention.

Link copied to clipboard

Whether the form field is valid or not.

Link copied to clipboard
open override val replayCache: List<T?>
Link copied to clipboard
val required: Boolean = true
Link copied to clipboard
open override val subscriptionCount: StateFlow<Int>
Link copied to clipboard
open override var value: T?

Functions

Link copied to clipboard
fun addTo(form: Form)

Convenient method to add this form field to a form.

Link copied to clipboard
fun addValidator(validator: Validator<T?>)

Add multiple validators to this form field.

Link copied to clipboard
fun addValidators(vararg validators: Validator<T?>)

Add multiple validators to this form field.

Link copied to clipboard
open suspend override fun collect(collector: FlowCollector<T?>): Nothing
Link copied to clipboard
open override fun compareAndSet(expect: T?, update: T?): Boolean
Link copied to clipboard
open suspend override fun emit(value: T?)
Link copied to clipboard
fun <T> FormField<T>.observe(scope: CoroutineScope, onValueChanged: (T?) -> Unit = {}, onFeedback: (Any?) -> Unit = {}, onError: (String?) -> Unit = {}, onFocusRequest: () -> Unit = {})
Link copied to clipboard
open override fun resetReplayCache()
Link copied to clipboard
open override fun tryEmit(value: T?): Boolean