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:- Form Class (
client/composables/lib/vForm/Form.js) - Core form state management - useFormInput Composable (
client/components/forms/useFormInput.js) - Input component logic - Form Components (
client/components/forms/) - Reusable input components
Creating a Basic Form
1. Initialize the Form
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 theuseFormInput 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 theuseFormInput 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
Validation: Combine client-side validation (required, email, etc.) with server-side validation for the best user experience.