Skip to main content
Forms in OpnForm handle both client-side validation and server-side API integration. They work seamlessly with TanStack Query mutations for optimal user experience.

Form System Overview

Our form system consists of three main parts:
  1. Form Class (client/composables/lib/vForm/Form.js) - Core form state management
  2. useFormInput Composable (client/components/forms/useFormInput.js) - Input component logic
  3. Form Components (client/components/forms/) - Reusable input components

Creating a Basic Form

1. Initialize the Form

The useForm composable creates a reactive form instance with:
  • Data binding - automatic two-way data binding
  • Validation state - tracks errors and validation status
  • Loading state - busy/successful flags for UI feedback
  • Error handling - automatic error extraction from API responses

2. Create the Form Template

3. Handle Form Submission

Form Input Components

Using Form Inputs

All form inputs accept a common set of props through the useFormInput composable:

Available Input Components

  • <TextInput> - Text, email, password, etc.
  • <TextareaInput> - Multi-line text
  • <CheckboxInput> - Checkboxes
  • <SelectInput> - Dropdown selects
  • … and more!

Custom Input Components

Create custom inputs using the useFormInput composable:

Validation & Error Handling

Client-Side Validation

Form validation happens automatically:

Server-Side Validation

The form automatically handles API validation errors:

Precognition (Live Validation)

Use real-time server validation as users type with Laravel Precognition.

Error States

Access validation state programmatically:

Integration with TanStack Query

Forms integrate seamlessly with TanStack Query mutations for optimal caching and state management:
See Data Fetching for more details on how mutations work with caching and optimistic updates.

Form State Management

Form Methods

Form State Properties

Complete Example

Here’s a complete working example based on the LoginForm component:

Best Practices

Form State Management: Always use the useForm composable rather than managing form state manually. It handles validation, errors, and loading states automatically.
Error Handling: Let the form handle API validation errors automatically. Only add custom error handling for specific business logic.
File Uploads: When handling file uploads, the form will automatically serialize data as FormData. Make sure your API endpoint accepts multipart/form-data.
Validation: Combine client-side validation (required, email, etc.) with server-side validation for the best user experience.