Skip to content

FormController API

The FormController is the object returned by the useForm composable, which is used by the HeadlessForm component and provides the following properties and methods:

📔 Example manipulating the form from outside here, at the bottom of the form

State Properties

PropertyTypeDescription
$isValidatingbooleanWhether the form is currently validating
$isSubmittingbooleanWhether the form is currently submitting
$isDirtybooleanWhether any field has been modified
$isTouchedbooleanWhether any field has been touched/focused

Form Values Management

MethodParametersReturnsDescription
values()-objectGets the current form values
setFieldValue(path, value, validate, stateChanges)path: string, value: any, validate?: boolean, stateChanges?: objectPromise<void>Sets a field value
getFieldValue(path)path: stringanyRetrieves the value of a field
reset()-voidResets form to initial state

Validation Methods

MethodParametersReturnsDescription
validate()-Promise<boolean>Validates all fields
validateField(path)path: stringPromise<boolean>Validates a specific field
errors()-objectGets all form errors by field path
setFieldErrors(path, errors)path: string, errors: string[]voidSets errors for a specific field
getFieldErrors(path)path: stringstring[]Gets errors for a specific field
setErrors(errors)errors: Record<string, string[]>voidSets errors for multiple fields

Field Management

MethodParametersReturnsDescription
getField(path)path: stringFieldControllerGets a field's state
removeField(path)path: stringvoidRemoves a field from the form
hasField(path)path: stringbooleanChecks if a field exists
setFieldFocused(path)path: stringvoidMarks a field as focused
setFieldBlurred(path)path: stringvoidMarks a field as blurred

Array Operations

MethodParametersReturnsDescription
add(path, index, item)path: string, index: number, item: anyvoidAdds an item to an array
remove(path, index)path: string, index: numbervoidRemoves an item from an array
move(path, fromIndex, toIndex)path: string, fromIndex: number, toIndex: numbervoidMoves an item in an array
sort(path, callback)path: string, callback: (a, b) => numbervoidSorts array items

You must ensure that the path holds an array or array-like object

Event Methods

MethodParametersReturnsDescription
on(event, handler)event: string, handler: Function{ off: Function }Subscribes to an event
off(event, handler)event: string, handler?: FunctionvoidUnsubscribes from an event
emit(event, data)event: string, data: anyvoidEmits a custom event

Field State

The field state is returned by calling form.getField(path)

Each field in the form has the following state properties:

PathTypeDescription
$errorsstring[]Validation error messages
$isDirtybooleanField has been modified
$isTouchedbooleanField has been focused/interacted with
$isValidatingbooleanField is currently validating
$isValidbooleanComputed property: field has no errors

Released under the MIT License