Comprehensive Reference
Master Aurelia 2 forms with comprehensive coverage of binding patterns, advanced collections, validation integration, and performance optimization for production applications.
Forms are the cornerstone of interactive web applications. Whether you're building simple contact forms, complex data-entry systems, or dynamic configuration interfaces, Aurelia provides a comprehensive and performant forms system. This guide covers everything from basic input binding to advanced patterns like collection-based form controls, dynamic form generation, and seamless validation integration.
Looking for focused guides? This is a comprehensive reference covering all form concepts. For more digestible, task-focused guides, check out:
Form Basics - Text inputs, textareas, number/date inputs
Collections - Checkboxes, radio buttons, select elements, Sets, Maps
Form Submission - Handling form submission, state management, auto-save
File Uploads - File input handling, validation, progress tracking
Advanced Patterns - Multi-step wizards, dynamic forms, conditional validation, form state management
Table of Contents
Understanding Aurelia's Form Architecture
Aurelia's forms system is built on sophisticated observer patterns that provide automatic synchronization between your view models and form controls. Understanding this architecture helps you build more efficient and maintainable forms.
Data Flow Architecture
Key Components:
Observers: Monitor DOM events and property changes
Bindings: Connect observers to view model properties
Collection Observers: Handle arrays, Sets, and Maps efficiently
Mutation Observers: Track dynamic DOM changes
Value Converters & Binding Behaviors: Transform and control data flow
Automatic Change Detection
Aurelia automatically observes:
Text inputs:
input,change,keyupeventsCheckboxes/Radio:
changeevents with array synchronizationSelect elements:
changeevents with mutation observationCollections: Array mutations, Set/Map changes
Object properties: Deep property observation
This means you typically don't need manual event handlers—Aurelia handles the complexity automatically while providing hooks for customization when needed.
Basic Input Binding
Aurelia provides intuitive two-way binding for all standard form elements. Let's start with the fundamentals and build toward advanced patterns.
Simple Text Inputs
The foundation of most forms is text input binding:
Textarea Binding
Textareas work identically to text inputs:
Number and Date Inputs
Browser form controls always provide string values unless you bind to their typed DOM properties. Use Aurelia's value-as-* bindings when you need numbers or dates in your view-model.
value-as-number binds to the input's valueAsNumber, so age is a number (or NaN when the field is empty/invalid). value-as-date binds to valueAsDate, giving you a Date | null. If you keep value.bind, the value remains a string—be sure to convert it before serializing to JSON for APIs.
Binding With Text and Textarea Inputs
Text Input
Binding to text inputs in Aurelia is straightforward:
You can also bind other attributes like placeholder:
Textarea
Textareas work just like text inputs, with value.bind for two-way binding:
Any changes to textAreaValue in the view model will show up in the <textarea>, and vice versa.
Advanced Collection Patterns
One of Aurelia's most powerful features is its sophisticated support for collection-based form controls. Beyond simple arrays, Aurelia supports Sets, Maps, and custom collection types with optimal performance characteristics.
Boolean Checkboxes
The simplest checkbox pattern binds to boolean properties:
Array-Based Multi-Select
For traditional multi-select scenarios, bind arrays to checkbox groups:
Set-Based Collections (Advanced)
For high-performance scenarios with frequent additions/removals, use Set collections:
Resource-Keyed Collections (Expert Level)
Per-Resource Permission Sets (Expert Level)
For complex key-value selections (e.g., multiple actions per resource), keep a Set per resource so each checkbox can reuse Aurelia's built-in collection handling:
Performance Considerations
Choose the right collection type for your use case:
Arrays: General purpose, good for small to medium collections
Sets: High-performance for frequent additions/removals, O(1) lookups
Record/Map of Sets: Model resource → actions (or similar hierarchies) cleanly
Custom Matchers: When object identity comparison isn't sufficient
Performance Tips:
Use Set for large collections with frequent changes
Implement efficient matcher functions for object comparison
Avoid creating new objects in templates—use computed properties
Consider virtualization for very large checkbox lists
Event Handling and Binding Behaviors
Aurelia provides sophisticated event handling and binding behaviors that give you precise control over when and how form data synchronizes. These features are crucial for building responsive, performant forms.
Advanced Event Timing with updateTrigger
By default, Aurelia uses appropriate events for each input type, but you can customize this behavior:
Rate Limiting with Debounce and Throttle
Control the frequency of updates to improve performance and user experience:
Signal-Based Reactive Updates
Signals provide cache invalidation and coordinated updates across components:
Dynamic Forms and Performance
Building performant, dynamic forms requires understanding Aurelia's observation system and applying optimization strategies for complex scenarios.
Dynamic Field Generation
Create forms that adapt their structure based on configuration:
Performance Optimization Strategies
Implement performance optimizations for large, complex forms:
Radio Button and Select Element Patterns
Aurelia provides comprehensive support for single-selection controls with sophisticated object binding and custom matching logic.
Advanced Radio Button Groups
Radio buttons with complex object handling and conditional logic:
Advanced Select Elements with Smart Filtering
Sophisticated select components with search, grouping, and virtual scrolling:
Form Submission Patterns
Modern web applications require sophisticated form submission strategies that handle success, failure, loading states, and complex business logic. Aurelia provides flexible patterns for all scenarios.
Basic Form Submission with State Management
Implement comprehensive submission state management for better user experience:
Multi-Step Form Submission
Handle complex multi-step forms with progress tracking and validation:
File Inputs and Upload Handling
Working with file uploads in Aurelia typically involves using the standard <input type="file"> element and handling file data in your view model. While Aurelia doesn’t provide special bindings for file inputs, you can easily wire up event handlers or use standard properties to capture and upload files.
Capturing File Data
In most cases, you’ll want to listen for the change event on a file input:
multiple: Allows selecting more than one file.accept="image/*": Restricts file selection to images (this can be changed to fit your needs).change.trigger="handleFileSelect($event)": Calls a method in your view model to handle the file selection event.
View Model Handling
You can retrieve the selected files from the event object in your view model:
Key Points:
Reading File Data:
input.filesreturns aFileList; converting it to an array (Array.from) makes it easier to iterate over.FormData: Using
FormDatato append files is a convenient way to send them to the server (via Fetch).Error Handling: Always check
response.okto handle server or network errors.Disabling the Button: In the HTML,
disabled.bind="!selectedFiles.length"keeps the button disabled until at least one file is selected.
Single File Inputs
If you only need a single file, omit multiple and simplify your logic:
Validation and Security
When handling file uploads, consider adding validation and security measures:
Server-side Validation: Even if you filter files by type on the client (accept="image/*"), always verify on the server to ensure the files are valid and safe.
File Size Limits: Check file sizes either on the client or server (or both) to prevent excessively large uploads.
Progress Indicators: For a better user experience, consider using XMLHttpRequest or the Fetch API with progress events (via third-party solutions or polyfills), so you can display an upload progress bar.
Validation Integration
Aurelia's validation system integrates seamlessly with forms through the & validate binding behavior and specialized validation components. This section covers practical validation patterns for production applications.
Basic Validation with & validate
The & validate binding behavior automatically integrates form inputs with Aurelia's validation system.
Advanced Validation Display
Use validation components for sophisticated error display:
Dynamic Validation Rules
Create validation rules that adapt to changing form conditions:
Real-time Validation Feedback
Provide immediate feedback with sophisticated validation timing:
For comprehensive validation documentation, see the dedicated Validation Guide.
Security and Best Practices
Security in forms is critical for protecting user data and preventing common web vulnerabilities. Aurelia provides the foundation for secure form implementations, but you must implement security best practices.
Input Validation and Sanitization
Always validate and sanitize user input on both client and server sides:
Rate Limiting and Abuse Prevention
Implement client-side rate limiting and abuse prevention:
Content Security Policy (CSP) Considerations
Implement CSP-friendly form patterns:
Accessibility Considerations
Building accessible forms ensures your application works for users with disabilities and meets WCAG guidelines. Aurelia provides excellent support for accessibility features.
Semantic Form Structure
Use proper semantic HTML and ARIA attributes:
Accessible Form Validation
Implement validation feedback that works with screen readers:
CSS for Accessibility
Include essential accessibility styles:
Testing Accessibility
Include accessibility testing strategies:
This comprehensive forms documentation provides production-ready patterns for all aspects of form development in Aurelia 2, from basic binding to advanced security and accessibility considerations. Each section includes real-world examples that you can adapt to your specific use cases.
Last updated
Was this helpful?