FormApi

Class: FormApi<TFormData, TFormValidator>

A class representing the Form API. It handles the logic and interactions with the form state.

Normally, you will not need to create a new FormApi instance directly. Instead, you will use a framework hook/function like useForm or createForm to create a new instance for you that uses your framework's reactivity model. However, if you need to create a new instance manually, you can do so by calling the new FormApi constructor.

Type Parameters

TFormData

TFormValidator extends Validator<TFormData, unknown> | undefined = undefined

Constructors

new FormApi()

ts
new FormApi<TFormData, TFormValidator>(opts?): FormApi<TFormData, TFormValidator>

Constructs a new FormApi instance with the given form options.

Parameters

opts?: FormOptions<TFormData, TFormValidator>

Returns

FormApi<TFormData, TFormValidator>

Defined in

packages/form-core/src/FormApi.ts:386

Properties

fieldInfo

ts
fieldInfo: Record<unknown extends TFormData ? string : TFormData extends readonly any[] & IsTuple<TFormData> ? PrefixTupleAccessor<TFormData<TFormData>, AllowedIndexes<TFormData<TFormData>, never>, []> : TFormData extends any[] ? PrefixArrayAccessor<TFormData<TFormData>, [any]> : TFormData extends Date ? never : TFormData extends object ? PrefixObjectAccessor<TFormData<TFormData>, []> : TFormData extends string | number | bigint | boolean ? "" : never, FieldInfo<TFormData, TFormValidator>>;

A record of field information for each field in the form.

Defined in

packages/form-core/src/FormApi.ts:375


options

ts
options: FormOptions<TFormData, TFormValidator> = {};

The options for the form.

Defined in

packages/form-core/src/FormApi.ts:359


state

ts
state: FormState<TFormData>;

The current state of the form.

Note: Do not use state directly, as it is not reactive. Please use form.useStore() utility to subscribe to state

Defined in

packages/form-core/src/FormApi.ts:371


store

ts
store: Store<FormState<TFormData>, (cb) => FormState<TFormData>>;

A TanStack Store instance that keeps track of the form's state.

Defined in

packages/form-core/src/FormApi.ts:363

Methods

deleteField()

ts
deleteField<TField>(field): void

Type Parameters

TField extends string | number

Parameters

field: TField

Returns

void

Defined in

packages/form-core/src/FormApi.ts:1105


getFieldInfo()

ts
getFieldInfo<TField>(field): FieldInfo<TFormData, TFormValidator>

Gets the field info of the specified field.

Type Parameters

TField extends string | number

Parameters

field: TField

Returns

FieldInfo<TFormData, TFormValidator>

Defined in

packages/form-core/src/FormApi.ts:1016


getFieldMeta()

ts
getFieldMeta<TField>(field): undefined | FieldMeta

Gets the metadata of the specified field.

Type Parameters

TField extends string | number

Parameters

field: TField

Returns

undefined | FieldMeta

Defined in

packages/form-core/src/FormApi.ts:1007


getFieldValue()

ts
getFieldValue<TField>(field): DeepValue<TFormData, TField, IsNullable<TFormData>>

Gets the value of the specified field.

Type Parameters

TField extends string | number

Parameters

field: TField

Returns

DeepValue<TFormData, TField, IsNullable<TFormData>>

Defined in

packages/form-core/src/FormApi.ts:1000


handleSubmit()

ts
handleSubmit(): Promise<void>

Handles the form submission, performs validation, and calls the appropriate onSubmit or onInvalidSubmit callbacks.

Returns

Promise<void>

Defined in

packages/form-core/src/FormApi.ts:952


insertFieldValue()

ts
insertFieldValue<TField>(
field,
index,
value,
opts?): Promise<void>

Inserts a value into an array field at the specified index, shifting the subsequent values to the right.

Type Parameters

TField extends string | number

Parameters

field: TField

index: number

value: DeepValue<TFormData, TField, IsNullable<TFormData>> extends any[] ? any[] & DeepValue<TFormData, TField, IsNullable<TFormData>>[number] : never

opts?: UpdateMetaOptions

Returns

Promise<void>

Defined in

packages/form-core/src/FormApi.ts:1137


mount()

ts
mount(): void

Returns

void

Defined in

packages/form-core/src/FormApi.ts:513


moveFieldValues()

ts
moveFieldValues<TField>(
field,
index1,
index2,
opts?): void

Moves the value at the first specified index to the second specified index within an array field.

Type Parameters

TField extends string | number

Parameters

field: TField

index1: number

index2: number

opts?: UpdateMetaOptions

Returns

void

Defined in

packages/form-core/src/FormApi.ts:1255


pushFieldValue()

ts
pushFieldValue<TField>(
field,
value,
opts?): void

Pushes a value into an array field.

Type Parameters

TField extends string | number

Parameters

field: TField

value: DeepValue<TFormData, TField, IsNullable<TFormData>> extends any[] ? any[] & DeepValue<TFormData, TField, IsNullable<TFormData>>[number] : never

opts?: UpdateMetaOptions

Returns

void

Defined in

packages/form-core/src/FormApi.ts:1119


removeFieldValue()

ts
removeFieldValue<TField>(
field,
index,
opts?): Promise<void>

Removes a value from an array field at the specified index.

Type Parameters

TField extends string | number

Parameters

field: TField

index: number

opts?: UpdateMetaOptions

Returns

Promise<void>

Defined in

packages/form-core/src/FormApi.ts:1190


replaceFieldValue()

ts
replaceFieldValue<TField>(
field,
index,
value,
opts?): Promise<void>

Replaces a value into an array field at the specified index.

Type Parameters

TField extends string | number

Parameters

field: TField

index: number

value: DeepValue<TFormData, TField, IsNullable<TFormData>> extends any[] ? any[] & DeepValue<TFormData, TField, IsNullable<TFormData>>[number] : never

opts?: UpdateMetaOptions

Returns

Promise<void>

Defined in

packages/form-core/src/FormApi.ts:1164


reset()

ts
reset(values?, opts?): void

Resets the form state to the default values. If values are provided, the form will be reset to those values instead and the default values will be updated.

Parameters

values?: TFormData

Optional values to reset the form to.

opts?

Optional options to control the reset behavior.

opts.keepDefaultValues?: boolean

Returns

void

Defined in

packages/form-core/src/FormApi.ts:580


resetFieldMeta()

ts
resetFieldMeta<TField>(fieldMeta): Record<TField, FieldMeta>

Type Parameters

TField extends string | number

Parameters

fieldMeta: Record<TField, FieldMeta>

Returns

Record<TField, FieldMeta>

Defined in

packages/form-core/src/FormApi.ts:1050


setErrorMap()

ts
setErrorMap(errorMap): void

Updates the form's errorMap

Parameters

errorMap: ValidationErrorMap

Returns

void

Defined in

packages/form-core/src/FormApi.ts:1279


setFieldMeta()

ts
setFieldMeta<TField>(field, updater): void

Updates the metadata of the specified field.

Type Parameters

TField extends string | number

Parameters

field: TField

updater: Updater<FieldMeta>

Returns

void

Defined in

packages/form-core/src/FormApi.ts:1035


setFieldValue()

ts
setFieldValue<TField>(
field,
updater,
opts?): void

Sets the value of the specified field and optionally updates the touched state.

Type Parameters

TField extends string | number

Parameters

field: TField

updater: Updater<DeepValue<TFormData, TField, IsNullable<TFormData>>>

opts?: UpdateMetaOptions

Returns

void

Defined in

packages/form-core/src/FormApi.ts:1074


swapFieldValues()

ts
swapFieldValues<TField>(
field,
index1,
index2,
opts?): void

Swaps the values at the specified indices within an array field.

Type Parameters

TField extends string | number

Parameters

field: TField

index1: number

index2: number

opts?: UpdateMetaOptions

Returns

void

Defined in

packages/form-core/src/FormApi.ts:1229


update()

ts
update(options?): void

Updates the form options and form state.

Parameters

options?: FormOptions<TFormData, TFormValidator>

Returns

void

Defined in

packages/form-core/src/FormApi.ts:536


validateAllFields()

ts
validateAllFields(cause): Promise<ValidationError[]>

Validates form and all fields in using the correct handlers for a given validation cause.

Parameters

cause: ValidationCause

Returns

Promise<ValidationError[]>

Defined in

packages/form-core/src/FormApi.ts:606


validateArrayFieldsStartingFrom()

ts
validateArrayFieldsStartingFrom<TField>(
field,
index,
cause): Promise<ValidationError[]>

Validates the children of a specified array in the form starting from a given index until the end using the correct handlers for a given validation type.

Type Parameters

TField extends string | number

Parameters

field: TField

index: number

cause: ValidationCause

Returns

Promise<ValidationError[]>

Defined in

packages/form-core/src/FormApi.ts:640


validateField()

ts
validateField<TField>(field, cause): ValidationError[] | Promise<ValidationError[]>

Validates a specified field in the form using the correct handlers for a given validation type.

Type Parameters

TField extends string | number

Parameters

field: TField

cause: ValidationCause

Returns

ValidationError[] | Promise<ValidationError[]>

Defined in

packages/form-core/src/FormApi.ts:679

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.

scarf analytics