Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

formapi.md

1,095 lines21.8 KB

Class: FormApi<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta>

Defined in: packages/form-core/src/FormApi.ts:884

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

• TOnMount extends undefined | FormValidateOrFn<TFormData>

• TOnChange extends undefined | FormValidateOrFn<TFormData>

• TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>

• TOnBlur extends undefined | FormValidateOrFn<TFormData>

• TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>

• TOnSubmit extends undefined | FormValidateOrFn<TFormData>

• TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>

• TOnDynamic extends undefined | FormValidateOrFn<TFormData>

• TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>

• TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>

• TSubmitMeta = never

Implements

  • FieldManipulator<TFormData, TSubmitMeta>

Constructors

new FormApi()

new FormApi<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta>(opts?): FormApi<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta>

Defined in: packages/form-core/src/FormApi.ts:981

Constructs a new FormApi instance with the given form options.

Parameters

opts?

FormOptions<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta>

Returns

FormApi<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta>

Properties

baseStore

baseStore: Store<BaseFormState<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer>>;

Defined in: packages/form-core/src/FormApi.ts:916


fieldInfo

fieldInfo: Record<DeepKeys<TFormData>, FieldInfo<TFormData>>;

Defined in: packages/form-core/src/FormApi.ts:950

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


fieldMetaDerived

fieldMetaDerived: Derived<Record<DeepKeys<TFormData>, AnyFieldMeta>>;

Defined in: packages/form-core/src/FormApi.ts:931


options

options: FormOptions<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta> = {};

Defined in: packages/form-core/src/FormApi.ts:902

The options for the form.


store

store: Derived<FormState<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer>>;

Defined in: packages/form-core/src/FormApi.ts:932

Accessors

state

Get Signature

get state(): FormState<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer>

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

Returns

FormState<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer>

Methods

clearFieldValues()

clearFieldValues<TField>(field, options?): void

Defined in: packages/form-core/src/FormApi.ts:2436

Clear all values within an array field.

Type Parameters

• TField extends string

Parameters

field

TField

options?

UpdateMetaOptions

Returns

void

Implementation of

FieldManipulator.clearFieldValues

deleteField()

deleteField<TField>(field): void

Defined in: packages/form-core/src/FormApi.ts:2245

Type Parameters

• TField extends string

Parameters

field

TField

Returns

void

Implementation of

FieldManipulator.deleteField

formId()

formId(): undefined | string

Defined in: packages/form-core/src/FormApi.ts:1332

Returns

undefined | string


getAllErrors()

getAllErrors(): object

Defined in: packages/form-core/src/FormApi.ts:2548

Returns form and field level errors

Returns

object

fields
fields: Record<DeepKeys<TFormData>, {
  errorMap: ValidationErrorMap;
  errors: unknown[];
}>;
form
form: object;
form.errorMap
errorMap: ValidationErrorMap<UnwrapFormValidateOrFn<TOnMount>, UnwrapFormValidateOrFn<TOnChange>, UnwrapFormAsyncValidateOrFn<TOnChangeAsync>, UnwrapFormValidateOrFn<TOnBlur>, UnwrapFormAsyncValidateOrFn<TOnBlurAsync>, UnwrapFormValidateOrFn<TOnSubmit>, UnwrapFormAsyncValidateOrFn<TOnSubmitAsync>, UnwrapFormValidateOrFn<TOnDynamic>, UnwrapFormAsyncValidateOrFn<TOnDynamicAsync>, UnwrapFormAsyncValidateOrFn<TOnServer>>;
form.errors
errors: (
  | UnwrapFormValidateOrFn<TOnMount>
  | UnwrapFormValidateOrFn<TOnChange>
  | UnwrapFormAsyncValidateOrFn<TOnChangeAsync>
  | UnwrapFormValidateOrFn<TOnBlur>
  | UnwrapFormAsyncValidateOrFn<TOnBlurAsync>
  | UnwrapFormValidateOrFn<TOnSubmit>
  | UnwrapFormAsyncValidateOrFn<TOnSubmitAsync>
  | UnwrapFormValidateOrFn<TOnDynamic>
  | UnwrapFormAsyncValidateOrFn<TOnDynamicAsync>
  | UnwrapFormAsyncValidateOrFn<TOnServer>)[];

getFieldInfo()

getFieldInfo<TField>(field): FieldInfo<TFormData>

Defined in: packages/form-core/src/FormApi.ts:2148

Gets the field info of the specified field.

Type Parameters

• TField extends string

Parameters

field

TField

Returns

FieldInfo<TFormData>


getFieldMeta()

getFieldMeta<TField>(field): undefined | AnyFieldMeta

Defined in: packages/form-core/src/FormApi.ts:2139

Gets the metadata of the specified field.

Type Parameters

• TField extends string

Parameters

field

TField

Returns

undefined | AnyFieldMeta

Implementation of

FieldManipulator.getFieldMeta

getFieldValue()

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

Defined in: packages/form-core/src/FormApi.ts:2132

Gets the value of the specified field.

Type Parameters

• TField extends string

Parameters

field

TField

Returns

DeepValue<TFormData, TField>

Implementation of

FieldManipulator.getFieldValue

handleSubmit()

Call Signature

handleSubmit(): Promise<void>

Defined in: packages/form-core/src/FormApi.ts:1991

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

Returns

Promise<void>

Implementation of
FieldManipulator.handleSubmit

Call Signature

handleSubmit(submitMeta): Promise<void>

Defined in: packages/form-core/src/FormApi.ts:1992

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

Parameters
submitMeta

TSubmitMeta

Returns

Promise<void>

Implementation of
FieldManipulator.handleSubmit

insertFieldValue()

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

Defined in: packages/form-core/src/FormApi.ts:2283

Type Parameters

• TField extends string

Parameters

field

TField

index

number

value

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

options?

UpdateMetaOptions

Returns

Promise<void>

Implementation of

FieldManipulator.insertFieldValue

mount()

mount(): () => void

Defined in: packages/form-core/src/FormApi.ts:1361

Returns

Function

Returns

void


moveFieldValues()

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

Defined in: packages/form-core/src/FormApi.ts:2407

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

Type Parameters

• TField extends string

Parameters

field

TField

index1

number

index2

number

options?

UpdateMetaOptions

Returns

void

Implementation of

FieldManipulator.moveFieldValues

parseValuesWithSchema()

parseValuesWithSchema(schema): 
  | undefined
  | {
  fields: Record<string, StandardSchemaV1Issue[]>;
  form: Record<string, StandardSchemaV1Issue[]>;
}

Defined in: packages/form-core/src/FormApi.ts:2612

Parses the form's values with a given standard schema and returns issues (if any). This method does NOT set any internal errors.

Parameters

schema

StandardSchemaV1<TFormData, unknown>

The standard schema to parse the form values with.

Returns

| undefined | { fields: Record<string, StandardSchemaV1Issue[]>; form: Record<string, StandardSchemaV1Issue[]>; }


parseValuesWithSchemaAsync()

parseValuesWithSchemaAsync(schema): Promise<
  | undefined
  | {
  fields: Record<string, StandardSchemaV1Issue[]>;
  form: Record<string, StandardSchemaV1Issue[]>;
}>

Defined in: packages/form-core/src/FormApi.ts:2624

Parses the form's values with a given standard schema and returns issues (if any). This method does NOT set any internal errors.

Parameters

schema

StandardSchemaV1<TFormData, unknown>

The standard schema to parse the form values with.

Returns

Promise< | undefined | { fields: Record<string, StandardSchemaV1Issue[]>; form: Record<string, StandardSchemaV1Issue[]>; }>


pushFieldValue()

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

Defined in: packages/form-core/src/FormApi.ts:2269

Pushes a value into an array field.

Type Parameters

• TField extends string

Parameters

field

TField

value

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

options?

UpdateMetaOptions

Returns

void

Implementation of

FieldManipulator.pushFieldValue

removeFieldValue()

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

Defined in: packages/form-core/src/FormApi.ts:2341

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

Type Parameters

• TField extends string

Parameters

field

TField

index

number

options?

UpdateMetaOptions

Returns

Promise<void>

Implementation of

FieldManipulator.removeFieldValue

replaceFieldValue()

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

Defined in: packages/form-core/src/FormApi.ts:2315

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

Type Parameters

• TField extends string

Parameters

field

TField

index

number

value

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

options?

UpdateMetaOptions

Returns

Promise<void>

Implementation of

FieldManipulator.replaceFieldValue

reset()

reset(values?, opts?): void

Defined in: packages/form-core/src/FormApi.ts:1466

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.

keepDefaultValues?

boolean

Returns

void


resetField()

resetField<TField>(field): void

Defined in: packages/form-core/src/FormApi.ts:2466

Resets the field value and meta to default state

Type Parameters

• TField extends string

Parameters

field

TField

Returns

void

Implementation of

FieldManipulator.resetField

resetFieldMeta()

resetFieldMeta<TField>(fieldMeta): Record<TField, AnyFieldMeta>

Defined in: packages/form-core/src/FormApi.ts:2189

resets every field's meta

Type Parameters

• TField extends string

Parameters

fieldMeta

Record<TField, AnyFieldMeta>

Returns

Record<TField, AnyFieldMeta>


setErrorMap()

setErrorMap(errorMap): void

Defined in: packages/form-core/src/FormApi.ts:2484

Updates the form's errorMap

Parameters

errorMap

FormValidationErrorMap<TFormData, UnwrapFormValidateOrFn<TOnMount>, UnwrapFormValidateOrFn<TOnChange>, UnwrapFormAsyncValidateOrFn<TOnChangeAsync>, UnwrapFormValidateOrFn<TOnBlur>, UnwrapFormAsyncValidateOrFn<TOnBlurAsync>, UnwrapFormValidateOrFn<TOnSubmit>, UnwrapFormAsyncValidateOrFn<TOnSubmitAsync>, UnwrapFormValidateOrFn<TOnDynamic>, UnwrapFormAsyncValidateOrFn<TOnDynamicAsync>, UnwrapFormAsyncValidateOrFn<TOnServer>>

Returns

void


setFieldMeta()

setFieldMeta<TField>(field, updater): void

Defined in: packages/form-core/src/FormApi.ts:2168

Updates the metadata of the specified field.

Type Parameters

• TField extends string

Parameters

field

TField

updater

Updater<AnyFieldMetaBase>

Returns

void

Implementation of

FieldManipulator.setFieldMeta

setFieldValue()

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

Defined in: packages/form-core/src/FormApi.ts:2205

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

Type Parameters

• TField extends string

Parameters

field

TField

updater

Updater<DeepValue<TFormData, TField>>

opts?

UpdateMetaOptions

Returns

void

Implementation of

FieldManipulator.setFieldValue

swapFieldValues()

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

Defined in: packages/form-core/src/FormApi.ts:2378

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

Type Parameters

• TField extends string

Parameters

field

TField

index1

number

index2

number

options?

UpdateMetaOptions

Returns

void

Implementation of

FieldManipulator.swapFieldValues

update()

update(options?): void

Defined in: packages/form-core/src/FormApi.ts:1396

Updates the form options and form state.

Parameters

options?

FormOptions<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta>

Returns

void


validateAllFields()

validateAllFields(cause): Promise<unknown[]>

Defined in: packages/form-core/src/FormApi.ts:1492

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

Parameters

cause

ValidationCause

Returns

Promise<unknown[]>

Implementation of

FieldManipulator.validateAllFields

validateArrayFieldsStartingFrom()

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

Defined in: packages/form-core/src/FormApi.ts:1522

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

Parameters

field

TField

index

number

cause

ValidationCause

Returns

Promise<unknown[]>

Implementation of

FieldManipulator.validateArrayFieldsStartingFrom

validateField()

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

Defined in: packages/form-core/src/FormApi.ts:1563

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

Type Parameters

• TField extends string

Parameters

field

TField

cause

ValidationCause

Returns

unknown[] | Promise<unknown[]>

Implementation of

FieldManipulator.validateField