Form Basics
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 assumes familiarity with Aurelia's binding system and template syntax. For fundamentals, see Template Syntax & Features first.
Quick Navigation
Basic Inputs - Text, textarea, number, date inputs
Collections - Checkboxes, radios, multi-select, arrays
Form Submission - Submit forms, handle events
File Uploads - Handle file inputs and uploads
Validation Plugin - Integrate with @aurelia/validation
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.
Data Flow Architecture
User Input → DOM Event → Observer → Binding → View Model → Reactive Updates
↑ ↓
Form Element ← DOM Update ← Binding ← Property Change ← View ModelKey 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.
Simple Text Inputs
The foundation of most forms is text input binding:
Key points:
Use
value.bindfor two-way bindingForm inputs default to two-way binding automatically
Computed properties (like
isFormValid) automatically update
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—convert it before serializing to JSON for APIs.
Input Types and Binding
Aurelia supports all HTML5 input types:
text
string
General text input
email
string
Email addresses
password
string
Password fields
number
string (use value-as-number for number)
Numeric input
tel
string
Phone numbers
url
string
Website URLs
search
string
Search queries
date
string (use value-as-date for Date)
Date selection
time
string
Time selection
datetime-local
string (use value-as-date for Date)
Date and time
color
string
Color picker
range
string (use value-as-number for number)
Slider input
Binding Modes
While value.bind is automatic two-way binding, you can be explicit:
When to use each:
.bind- Default, use for most form inputs.two-way- Explicit two-way, same as.bindfor inputs.to-view- Read-only inputs, display-only values.from-view- Capture input without updating view.one-time- Static initial values
Real-World Example
Here's a complete user registration form:
Next Steps
Now that you understand basic form inputs, explore:
Collections - Checkboxes, radio buttons, and multi-select
Validation Plugin - Integrate form validation
File Uploads - Handle file inputs
Form Submission - Submit and process forms
Template Recipes - Real-world examples
Quick Tips
Always use labels - Associate labels with inputs using
forattributeValidate on submit - Don't validate every keystroke unless needed
Provide feedback - Show errors clearly after user completes input
Use computed properties - Let Aurelia handle form state reactively
Keep it simple - Don't overcomplicate with manual DOM manipulation
Common Pitfalls
Forgetting
.bind- Must use.bindfor two-way bindingType mismatches - Number inputs return strings, convert if needed
Direct object mutation - Use
this.form.prop = value, notform[prop] = valueMissing labels - Always include labels for accessibility
Related Documentation
Last updated
Was this helpful?