--- id: fieldApi title: Field API --- ### Creating a new FieldApi Instance > Some of these docs may be inaccurate due to an API shift in `0.11.0`. If you're interested in helping us fix these issues, please [join our Discord](https://tlinz.com/discord) and reach out in the `#form` channel. Normally, you will not need to create a new `FieldApi` instance directly. Instead, you will use a framework hook/function like `useField` or `createField` to create a new instance for you that utilizes your frameworks reactivity model. However, if you need to create a new instance manually, you can do so by calling the `new FieldApi` constructor. ```tsx const fieldApi: FieldApi = new FieldApi(formOptions: Field Options) ``` ### `FieldOptions` An object type representing the options for a field in a form. - ```tsx name ``` - The field name. If `TFormData` is `unknown`, the type will be `string`. Otherwise, it will be `DeepKeys`. - ```tsx defaultValue?: TData ``` - An optional default value for the field. - ```tsx form?: FormApi ``` - An optional reference to the form API instance. - ```tsx validate?: (value: TData, fieldApi: FieldApi) => ValidationError | Promise ``` - An optional validation function for the field. - ```tsx validatePristine?: boolean ``` - An optional flag indicating whether to validate the field when it is pristine (untouched). - ```tsx defaultMeta?: Partial ``` - An optional object with default metadata for the field. - ```tsx validateOn?: ValidationCause ``` - An optional string indicating when to perform field validation. - ```tsx validateAsyncOn?: ValidationCause ``` - An optional string indicating when to perform async field validation. - ```tsx validateAsyncDebounceMs?: number ``` - If set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds. ### `ValidationCause` A type representing the cause of a validation event. - 'change' | 'blur' | 'submit' ### `FieldMeta` An object type representing the metadata of a field in a form. - ```tsx isTouched: boolean ``` - A flag indicating whether the field has been touched. - ```tsx touchedError?: ValidationError ``` - An optional error related to the touched state of the field. - ```tsx error?: ValidationError ``` - An optional error related to the field value. - ```tsx isValidating: boolean ``` - A flag indicating whether the field is currently being validated. ### `FieldApiOptions` An object type representing the required options for the `FieldApi` class. - Inherits from `FieldOptions` with the `form` property set as required. ### `FieldApi` A class representing the API for managing a form field. #### Properties - ```tsx uid: number ``` - A unique identifier for the field instance. - ```tsx form: FormApi ``` - A reference to the form API instance. - ```tsx name: DeepKeys ``` - The field name. - ```tsx store: Store> ``` - The field state store. - ```tsx state: FieldState ``` - The current field state. - ```tsx options: RequiredByKey, 'validateOn'> ``` - The field options with the `validateOn` property set as required. #### Methods - ```tsx constructor(opts: FieldApiOptions) ``` - Initializes a new `FieldApi` instance. - ```tsx mount(): () => void ``` - Mounts the field instance to the form. - ```tsx updateStore(): void ``` - Updates the field store with the latest form state. - ```tsx update(opts: FieldApiOptions): void ``` - Updates the field instance with new options. - ```tsx getValue(): TData ``` - Gets the current field value. - ```tsx setValue(updater: Updater, options?: { touch?: boolean; notify?: boolean }): void ``` - Sets the field value. - ```tsx getMeta(): FieldMeta ``` - Gets the current field metadata. - ```tsx setMeta(updater: Updater): void ``` - Sets the field metadata. - ```tsx getInfo(): any ``` - Gets the field information object. - ```tsx pushValue(value: TData): void ``` - Pushes a new value to the field. - ```tsx insertValue(index: number, value: TData): void ``` - Inserts a value at the specified index. - ```tsx removeValue(index: number): void ``` - Removes a value at the specified index. - ```tsx swapValues(aIndex: number, bIndex: number): void ``` - Swaps the values at the specified indices. - ```tsx getSubField>(name: TName): FieldApi, TFormData> ``` - Gets a subfield instance. - ```tsx validate(): Promise ``` - Validates the field value. - ```tsx getChangeProps>(props: T = {} as T): ChangeProps & Omit> ``` - Gets the change and blur event handlers. - ```tsx getInputProps(props: T = {} as T): InputProps & Omit ``` - Gets the input event handlers. ### `FieldState` An object type representing the state of a field. - ```tsx value: TData ``` - The current value of the field. - ```tsx meta: FieldMeta ``` - The current metadata of the field. ### `UserChangeProps` An object type representing the change and blur event handlers for a field. - ```tsx onChange?: (value: TData) => void ``` - An optional function to further handle the change event. - ```tsx onBlur?: (event: any) => void ``` - An optional function to further handle the blur event. ### `UserInputProps` An object type representing the input event handlers for a field. - ```tsx onChange?: (event: any) => void ``` - An optional function to further handle the change event. - ```tsx onBlur?: (event: any) => void ``` - An optional function to further handle the blur event. ### `ChangeProps` An object type representing the change and blur event handlers for a field. - ```tsx value: TData ``` - The current value of the field. - ```tsx onChange: (value: TData) => void ``` - A function to handle the change event. - ```tsx onBlur: (event: any) => void ``` - A function to handle the blur event. ### `InputProps` An object type representing the input event handlers for a field. - ```tsx value: string ``` - The current value of the field, coerced to a string. - ```tsx onChange: (event: any) => void ``` - A function to handle the change event. - ```tsx onBlur: (event: any) => void ``` - A function to handle the blur event.