Getting Started with Formily
Formily is a powerful, unified form solution that provides a complete ecosystem for building complex forms in React applications.
What is Formily?
Formily is not just another form library - it’s a comprehensive form solution that includes:
- Core Libraries: Reactive state management and form logic
- UI Bridges: Framework-specific integrations (React, Vue)
- Component Ecosystem: Pre-built components for popular UI libraries
- Developer Tools: Form designer and Chrome extension
Architecture Overview
Formily follows the MVVM (Model-View-ViewModel) pattern:
- @formily/core: The core form state management engine
- @formily/react: React bridge for connecting UI components
- @formily/antd: Ant Design component integration
- Additional packages: Support for various UI libraries
Installation
Step 1: Install Core Library
The core library is essential for all Formily applications:
1 | |
Step 2: Install UI Bridge
For React applications:
1 | |
For Vue applications:
1 | |
Step 3: Install Component Library
Choose based on your preferred UI library:
For Ant Design:
1 | |
For Alibaba Fusion:
1 | |
Basic Usage
Let’s start with a simple example to understand Formily’s core concepts:
1 | |
Key Concepts Explained
1. Form Creation with createForm
The createForm function creates the core form model that manages:
- Form state
- Validation rules
- Field relationships
- Submission logic
1 | |
2. FormProvider Component
FormProvider acts as the bridge between the form model and React components:
1 | |
3. Field Component
The Field component is the building block for form fields:
1 | |
4. Decorator vs Component
- Decorator: Wraps the field with UI elements (labels, validation messages)
- Component: The actual input control (Input, Select, etc.)
1 | |
5. Reactive Updates with FormConsumer
FormConsumer enables reactive UI updates based on form state changes:
1 | |
Advanced Features
Form Validation
Formily provides multiple validation approaches:
1 | |
Dynamic Forms
Create dynamic forms that respond to user input:
1 | |
Form Layout
Control form layout with FormLayout:
1 | |
Best Practices
1. Form State Management
Keep form logic separate from component logic:
1 | |
2. Reusable Field Components
Create reusable field components:
1 | |
3. Form Submission Handling
Handle form submission properly:
1 | |
Ecosystem and Tools
Form Designer
Formily provides a visual form designer for building forms without code:
- Designable: Visual form builder
- Chrome Extension: Development tools for debugging
- Community Components: Extended component library
UI Library Support
Formily supports multiple UI libraries:
- Ant Design:
@formily/antdand@formily/antd-v5 - Element UI:
@formily/elementand@formily/element-plus - Fusion Design:
@formily/next - Mobile:
@formily/antd-mobile,@formily/vant
Conclusion
Formily offers a powerful, scalable solution for form management in React applications. Its reactive architecture, comprehensive validation system, and extensive ecosystem make it an excellent choice for complex form requirements.
Key benefits include:
- Reactive State Management: Automatic UI updates based on form state
- Comprehensive Validation: Built-in and custom validation rules
- Rich Ecosystem: Support for popular UI libraries
- Developer Experience: Excellent tooling and documentation
- Performance: Optimized rendering and state updates
Whether you’re building simple contact forms or complex multi-step wizards, Formily provides the tools and flexibility needed to create exceptional form experiences.
Resources
Start exploring Formily today and transform your form development experience!