-
-
Notifications
You must be signed in to change notification settings - Fork 2
API Reference ‐ Form
The main Form
class provides comprehensive form state management and submission capabilities.
constructor(initialData: TForm, axiosInstance: AxiosInstance = axios)
Creates a new form instance.
-
Parameters:
-
initialData: TForm
- Initial form data -
axiosInstance?: AxiosInstance
- Optional custom Axios instance (defaults to global axios)
-
- Returns: Proxied Form instance
-
data: TForm
- Current form data -
defaults: TForm
- Default form values -
isDirty: boolean
- Whether form data has been modified from defaults
-
processing: boolean
- Whether form is currently being submitted -
wasSuccessful: boolean
- Whether last submission was successful -
recentlySuccessful: boolean
- Whether form was successfully submitted in last 2 seconds
-
progress: Progress | null
- Upload progress informationinterface Progress { total: number; loaded: number; percentage: number; bytes: number; lengthComputable: boolean; }
-
errors: Partial<Record<keyof TForm | 'formError', string>>
- Form validation errors
Submit the form with specified HTTP method.
-
Parameters:
-
method: Method
- HTTP method ('get', 'post', 'put', 'patch', 'delete', 'options') -
url: string
- Submission URL -
options?: Partial<FormOptions<TForm>>
- Submission options
-
-
Options Interface:
interface FormOptions<TForm> { onBefore?: () => void; onProgress?: (progress: Progress) => void; onSuccess?: (response: AxiosResponse) => void; onError?: (errors: Partial<Record<keyof TForm | 'formError', string>>) => void; onFinish?: () => void; }
get(url: string, options?: Partial<FormOptions<TForm>>): Promise<void>
post(url: string, options?: Partial<FormOptions<TForm>>): Promise<void>
put(url: string, options?: Partial<FormOptions<TForm>>): Promise<void>
patch(url: string, options?: Partial<FormOptions<TForm>>): Promise<void>
delete(url: string, options?: Partial<FormOptions<TForm>>): Promise<void>
options(url: string, options?: Partial<FormOptions<TForm>>): Promise<void>
submitDebounced(method: Method, url: string, options?: Partial<FormOptions<TForm>>): void
Submit the form with a 300ms debounce.
Set error message for specific field.
-
Parameters:
-
field: keyof TForm
- Form field name -
message: string
- Error message
-
Set multiple error messages.
-
Parameters:
-
errors: Partial<Record<keyof TForm | 'formError', string>>
- Object mapping fields to error messages
-
Clear all form errors.
Reset form data to defaults.
-
Parameters:
-
...fields: (keyof TForm)[]
- Optional specific fields to reset. If none provided, resets all fields.
-
Set new default values.
-
Parameters:
-
fieldOrFields?: keyof TForm | Partial<TForm>
- Field(s) to set as defaults -
value?: FormDataConvertible
- Value for single field
-
Transform form data before submission.
-
Parameters:
-
callback: (data: TForm) => object
- Transformation function
-
- Returns: Form instance for chaining
Validate form data against defined rules.
- Returns: Promise resolving to boolean indicating validation success
Cancel ongoing form submission.
Clean up form instance and remove all data.
type Method = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'options'
interface Progress {
total: number;
loaded: number;
percentage: number;
bytes: number;
lengthComputable: boolean;
}
interface FormOptions<TForm> {
onBefore?: () => void;
onProgress?: (progress: Progress) => void;
onSuccess?: (response: AxiosResponse) => void;
onError?: (errors: Partial<Record<keyof TForm | 'formError', string>>) => void;
onFinish?: () => void;
}
interface ApiValidationError {
message: string;
errors: Record<string, string[]>;
}
The library automatically detects when form data contains files and converts the data to FormData
format when necessary. File upload progress is tracked and reported through the progress
property and onProgress
callback.
The library automatically includes CSRF tokens from the page's meta tags:
<meta name="csrf-token" content="token-value">
The Form class uses a Proxy to provide direct access to form fields:
- Get operations check form data first, then instance properties
- Set operations enforce field name validation and track dirty state
- Reserved field names are protected from accidental overwriting
The library handles different types of errors:
- HTTP 422 responses are treated as validation errors
- Cancelled requests are not treated as errors
- Other errors are captured in the
formError
field - All errors are available through the
errors
property andonError
callback