Form

A form object that contains the core library logic.

Properties

values

{ [key: string]: any }

This is a getter that returns current values

touched

{ [path: string]: boolean }

This is a getter that returns current touched. These values are needed when you want to show errors only after the user has touched the field. Usually marked as touched after calling the onBlur event. The values are flat, see detail.

errors

{ [path: string]: string }

This is a getter that returns current errors. The values are flat, see detail.

isValid

boolean

Methods

setFieldValue

setFieldValue(name: string, value: any): void

Sets a new value, starts validations and notifies all subscribers. For nested values, use name as described here.

setFieldTouched

setFieldTouched(name: string, isTouched?: boolean): void

Marks the field as touched or not and notifies all subscribers. Value by default is true. For nested values, use name as described here.

setFieldError

setFieldError(name: string, message: string): void

This sets an external error for the field, marks the field as touched, and notifies all subscribers. After changing the field value, this error is reset. For nested values, use name as described here.

touchAllFields

touchAllFields(): void

Marks all fields with errors as touched and notifies all subscribers. This is useful after trying to submit a form.

reset

reset(): void

Sets the initial values for the values, touched, errors, and notifies all subscribers.

subscribe

Listener(): void
Unsubscribe(): void

subscribe(listener: Listener): Unsubscribe

Adds a subscriber and returns the unsubscribe function.

Please note: the list of subscribers remains unchanged until all subscribers are called. You should keep this in mind when one of the subscribers may initiate the unsubscription of another subscriber.

Last updated