Form Hook

useForm() Hook

Sets up the form hook.

Signature:
function useForm<Values extends FormValues>(
initialValues: Values,
onSubmit: (values: Values) => void | Promise<void>,
options: FormOptions<Values> = {}
): FormiteForm<Values>

Parameters

ParameterTypeDescription
initialValuesValuesInitial values of the form
onSubmit(values: Values) => void ǀ Promise<void>The function that is called when submitting the form.
optionsFormOptions<Values>Optional Form options
Returns:

FormiteForm<Values>

The forms state and API

Remarks

All initial field values should be set even if they are undefined.

Form Options

Options for the Form Hook.

Signature:
type FormOptions<Values extends FormValues> = {
validateInitialValues?: boolean;
validateOnBlur?: boolean;
validateOnChange?: boolean;
onValidate?: ValidateFormHandler<Values>;
};
OptionDescription
validateInitialValuesOption to validate the initial values. Default is true.
validateOnBlurOption to validate the field and form when an element looses focus. Default is true.
validateOnChangeOption to validate the field and form when a field's value changes. Default is true.
onValidateA callback function to validate the form.

Form Validation

Function to validate the entire form.

Signature:
type ValidateFormHandler<VALUES> = (
values: VALUES,
fields: Fields<VALUES>,
setFieldError: (field: Field, error?: string) => void
) => string[] | Promise<string[]>;

Remarks

The function receives the form's current values and should return an empty array if the values are valid. Otherwise the function should return an array of error messages. You can also set errors on individual fields by calling setFieldError(). If validation is asynchronous the function should return a Promise.

Form State and API

State and API of the form returned by the useForm() hook.

Signature:
interface FormiteForm<Values extends FormValues = FormValues> {
readonly canSubmit: boolean;
readonly fields: Fields<Values>;
readonly formErrors: string[];
readonly isDirty: boolean;
readonly isSubmitting: boolean;
readonly isValid: boolean;
readonly isValidating: boolean;
reset: () => void;
setFieldTouched: (field: Field, touched: boolean) => void;
setFieldValue: (field: Field, v: any, validate?: boolean) => Promise<boolean>;
submit: () => Promise<boolean>;
updateFields: (updateAction: (newFields: Fields<Values>) => void) => void;
validate: () => Promise<boolean>;
}

Properties

PropertyTypeDescription
canSubmitbooleantrue if the form is valid and is not already submitting.
fieldsFields<Values>The form's fields with the same structure as the initial values.
formErrorsstringCurrent form errors if there are any or an empty array.
isDirtybooleantrue if any of the fields' values have changed.
isSubmittingbooleantrue while the form is submitting.
isValidbooleantrue if the form and all fields are valid.
isValidatingbooleantrue while the form and fields are validating.
reset() => voidSets all fields to the initial values and resets the form's state.
setFieldTouched(field: Field, touched: boolean) => voidProgrammatically 'touches' a field.
setFieldValue(field: Field, v: any, validate?: boolean) => Promise<boolean>Programmatically sets a field's value.
submit() => Promise<boolean>Submits the form if it is valid. Returns a Promise that resolves to true on success.
updateFields(updateAction: (newFields: Fields<Values>) => void) => voidAllows to changes the form's fields programmatically. Used for dynamic forms.
validate() => Promise<boolean>Validates the form and all fields and returns a Promise that resolves to true if the form and all fields are valid.

useField() Hook

Sets up a form's field hook.

Signature:
function useField(
field: Field,
onValidate?: ValidateFieldHandler,
metadata?: any
): FormiteField

Parameters

ParameterTypeDescription
fieldFieldThe Field to connect to an input component.
onValidateValidateFieldHandlerAn optional callback function to validate the field.
metadataanyAn optional data value that is stored with the Field.
Returns:

FormiteField

The fields state and API.

Field Validation

Function to validate a single field's value.

Signature:
type ValidateFieldHandler = (value: any, field: Field) => string | undefined | Promise<string | undefined>;

Remarks

The function receives the current value of the field and should return undefined or an empty string if the value is valid. Otherwise the function should return an error message. If validation is asynchronous the function should return a Promise.

Field State and API

State and API of an individual field returned by the useField() hook.

Signature:
interface FormiteField {
readonly value: any;
onBlur: () => void;
onChange: (value: any) => void;
}

Properties

PropertyTypeDescription
onBlur() => voidHandler that should be passed to an element's onBlur event.
onChange(value: any) => voidHandler that should be passed to or called from an element's onChange event.
valueanyCurrent value of the field

Field

A form's field with its current state.

Signature:
class Field<T = any> {
readonly error: string | undefined;
readonly initialValue: T;
readonly isValidating: boolean;
readonly name: string;
readonly touched: boolean;
readonly value: T;
readonly visibleError: string | undefined;
metadata: any;
}

Properties

PropertyTypeDescription
errorstring ǀ undefinedAn error message if the field is not valid.
initialValueTThe initial value that was provided to the Form Hook.
isValidatingbooleantrue while the field is validating.
metadataanyCustom data value. For example, to be used within the validation function.
namestring
touchedbooleantrue after a field's input component lost focus.
valueTThe current value of the field
visibleErrorstring ǀ undefinedThe error message if the field is not valid and after the field has been touched.

Remarks

The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the Field class.

createFields() function

Creates Field instances for all properties of the given object.

Signature:
function createFields<VALUES extends FormValues>(object: VALUES): Fields<VALUES>;

Parameters

ParameterTypeDescription
objectVALUESAn object whose properties will be converted to Field instances.
Returns:

Fields<VALUES>

An object with fields and arrays of fields with the same structure as the input object

Remarks

Makes a deep copy of all values and arrays and creates new Field instances. This function is only used when using dynamic forms.

clearFieldErrors() function

Clears the errors of the fields by doing a deep traversal.

Signature:
function clearFieldErrors<VALUES extends FormValues>(fields: Fields<VALUES>): void;

Parameters

ParameterTypeDescription
fieldsFields<VALUES>A Field, an object with fields or an array of fields

Remarks

For example, it can be used during form validation to clear all errors before setting field errors.