Skip to content

📖 API Reference

createForm(initialValues, rulesBuilder, options?)

Creates a reactive form instance with integrated validation.

Parameters:

  • initialValues -Initial form values (supports type inference)
  • rulesBuilder - A rule builder function (r, define) => define({...}) or a reactive computed(() => { const r = createRules(); return {...} }) for i18n support
  • options - Optional configuration object

Configuration Options:

  • onSubmit? - Callback triggered upon successful form submission
  • onClear? - Callback triggered when the form is cleared

Returns: A form instance with reactive properties and utility methods

Key Features:

  • Supports nested paths like 'contacts.0.email'
  • Type-safe path construction via arrayPath() and objectPath()
  • Built-in array management: addArrayItem(), removeArrayItem(), toggleArrayItem()
  • Automatic performance optimization based on data structure

Form Properties and Methods

Reactive State

PropertyTypeDescription
valuesRef<T>Reactive reference to current form values
valTGetter for convenient value access (from script)
errorsRef<Record<string, string[]>>Validation errors indexed by field name
touchedRef<Record<string, boolean>>Tracks which fields have been interacted with
dirtyRef<Record<string, boolean>>Tracks which fields have been modified
isValidatingRef<Record<string, boolean>>Indicates fields currently undergoing async validation
isSubmittingRef<boolean>Global form submission status
isValidComputedRef<boolean>true if the entire form is valid
isDirtyComputedRef<boolean>true if any field has unsaved changes
hasAnyErrorsComputedRef<boolean>true if there is at least one validation error
touchedFieldsComputedRef<string[]>List of all touched field paths
dirtyFieldsComputedRef<string[]>List of all modified field paths

Validation Methods

MethodDescription
setRules(rules)Updates validation rules dynamically
validateField(name)Manually triggers validation for a specific field (supports nested paths)
validateForm()Triggers validation for all form fields
submit()Validates the form and triggers onSubmit if successful
touch(field)Marks a field (flat or nested) as "touched"

State Management

MethodDescription
setValues(values)Updates field values
getValues()Returns a deep copy of current values
clear(useInitial?)Clears the form data
reset(newValues?)Resets the form to initial or provided values
resetState()Resets the validation state (touched, dirty, etc.)
setErrors(errors)Manually sets errors for specific fields
resetErrors()Clears all current validation errors

Field Status Checks

Unified Methods (compatible with flat and nested paths):

MethodReturnsDescription
hasError(field)booleanChecks if a field has any errors
error(field)string | nullReturns the first error message for a field
allErrors(field)string[]Returns all error messages for a field
isTouched(field)booleanChecks if a field has been touched
validating(field)booleanChecks if a field is currently being validated
isFieldDirty(field)booleanChecks if a field's value has changed
getFieldStatus(field)FieldStatusReturns the complete status object for a field

Usage Examples:

typescript
// Standard fields
form.hasError('email')
form.error('name')

// Nested paths
form.hasError('contacts.0.email')
form.error('address.street')

// Type-safe path helpers
form.hasError(form.arrayPath('contacts', 0, 'email'))
form.error(form.objectPath('address', 'street'))

Nested Structure Management

MethodDescription
addArrayItem(arrayPath, item)Appends an item to a target array
removeArrayItem(arrayPath, index)Removes an item from an array at a specific index
toggleArrayItem(arrayPath, item)Adds an item if it doesn't exist, otherwise removes it
arrayIncludes(arrayPath, item)Checks if an array contains a specific item
arrayPath(arrayField, index, property)Generates a type-safe path for an array element
objectPath(objectField, property)Generates a type-safe path for an object property

File Utilities

PropertyDescription
file.{fieldName}.filesComputedRef<File[]> - List of selected files
file.{fieldName}.fileInfoComputedRef<FileInfo[]> - Detailed file metadata
file.{fieldName}.handler(event: Event) => void - Event handler for file inputs
file.{fieldName}.clear() => void - Resets selected files and the DOM input

Note: Helpers are lazily initialized. For multiple file selection, ensure the <input type="file"> has the multiple attribute; the library detects this automatically from the input event.

Important: The clear() method synchronizes the form state with the UI by resetting the actual DOM input element value, preventing "ghost" file names from appearing in the input after they were removed from the form.

Advanced Methods

MethodDescription
clearCache(field?)Clears the validation cache for a specific field or the entire form
dispose()Stops all watchers and releases resources (called automatically on unmount)

Released under the MIT License.