For the complete documentation index, see llms.txt. This page is also available as Markdown.

Form inputs

Aurelia provides a powerful and intuitive approach to handling form inputs and user interactions. At the core of Aurelia's form handling is two-way binding, which creates a seamless connection between the view (HTML) and the view model (JavaScript).

Key Characteristics of Aurelia Form Inputs

  • Automatic Two-Way Binding: By default, form elements automatically sync changes between the view and view model

  • Reactive Data Flow: Input changes are immediately reflected in the view model

  • Minimal Boilerplate: Requires less code compared to traditional form handling approaches

Data Flow Mechanism

When a user interacts with a form input, the following process occurs:

  1. User enters/modifies input value

  2. Native form input events are triggered

  3. Aurelia's binding system detects the change

  4. View model is automatically updated

  5. Any dependent properties or computations are instantly refreshed

Basic Form Input Binding

Text Inputs

<template>
  <input type="text" value.bind="firstName">
  <p>Hello, ${firstName}</p>
</template>

Input Binding Variations

Binding Mode
Syntax
Description

Two-Way Binding

value.bind

Default, syncs changes in both directions

One-Way Binding

value.one-way

Updates view model, but not vice versa

One-Time Binding

value.one-time

Initial value only

Textarea

Checkbox Inputs

Radio Buttons

Select Dropdowns

Input Binding Considerations

Note: When using model.bind, ensure you're binding to the entire object, not just a primitive value. This allows for more complex data binding scenarios.

Tip: Always initialize form-related properties in your view model to prevent undefined errors and provide default states.

Advanced Input Techniques

Custom Validation

Aurelia provides flexible validation through the aurelia-validation plugin. Here's a comprehensive approach to form validation:

Input Formatting

Number Formatting

Conditional Binding

Computed Properties with Inputs

Validation and Error Handling

Technique
Description
Use Case

Required Fields

Ensure input is not empty

Form completeness

Email Validation

Check email format

User registration

Number Range

Validate numeric inputs

Age, quantity limits

Custom Validators

Complex validation logic

Specialized form rules

Use meaningful error messages that guide users to correct their input.

Form Submission

Form submission in Aurelia involves managing user input, validation, and processing data with a clean, reactive approach. The framework provides multiple strategies for handling form submissions efficiently.

Basic Form Submission

Comprehensive Form Validation

Preventing Default Form Submission

Handling Form Errors

Always prevent default form submission and handle it programmatically to ensure full control over the process.

Working with Complex Forms

Complex Object Binding

Nested Form Structures

Handling Arrays of Form Data

Practical Examples

User Registration Form

Break complex forms into logical sections for improved maintainability.

Last updated

Was this helpful?