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.

circle-info

This guide assumes familiarity with Aurelia's binding system and template syntax. For fundamentals, see Template Syntax & Features first.

Quick Navigation

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 Model

Key Components:

  1. Observers: Monitor DOM events and property changes

  2. Bindings: Connect observers to view model properties

  3. Collection Observers: Handle arrays, Sets, and Maps efficiently

  4. Mutation Observers: Track dynamic DOM changes

  5. Value Converters & Binding Behaviors: Transform and control data flow

Automatic Change Detection

Aurelia automatically observes:

  • Text inputs: input, change, keyup events

  • Checkboxes/Radio: change events with array synchronization

  • Select elements: change events with mutation observation

  • Collections: 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.bind for two-way binding

  • Form 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:

Type
Value
Common Use

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 .bind for 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:

Quick Tips

  1. Always use labels - Associate labels with inputs using for attribute

  2. Validate on submit - Don't validate every keystroke unless needed

  3. Provide feedback - Show errors clearly after user completes input

  4. Use computed properties - Let Aurelia handle form state reactively

  5. Keep it simple - Don't overcomplicate with manual DOM manipulation

Common Pitfalls

  • Forgetting .bind - Must use .bind for two-way binding

  • Type mismatches - Number inputs return strings, convert if needed

  • Direct object mutation - Use this.form.prop = value, not form[prop] = value

  • Missing labels - Always include labels for accessibility

Last updated

Was this helpful?