Validation
Introduction
Aurelia's validation plugin provides a robust and flexible form and data validation system. It offers a fluent, declarative API that makes it easy to define validation rules for your application's models and forms.
The validation system supports:
Declarative rule definition
Customizable validation triggers
Flexible error rendering
Extensive rule types
Internationalization support
Key Features
Fluent rule configuration
Multiple validation triggers
Custom validation rules
Easy error display
Seamless integration with Aurelia's binding system
Installation and Setup
Installation
Install the validation plugin using npm or your chosen package installer:
Plugin Configuration
In your application's main configuration file (typically main.js
or main.ts
), register the validation plugin:
Optional Configuration
You can customize the default validation trigger during plugin registration:
Validation Basics
Defining Rules
Aurelia's validation uses a fluent API to define validation rules. The primary class for defining rules is ValidationRules
.
Basic Rule Definition
Rule Types
Aurelia provides several built-in validation rules:
required()
Ensures the property is not null, undefined, or whitespace
.required()
email()
Validates email format
.email()
minLength(length)
Validates minimum string length
.minLength(3)
maxLength(length)
Validates maximum string length
.maxLength(50)
matches(regex)
Validates against a regular expression
.matches(/^\d+$/)
min(value)
Validates minimum numeric value
.min(0)
max(value)
Validates maximum numeric value
.max(100)
Custom Display Names
You can set custom display names for more meaningful error messages:
Rule Sequencing
Rules are evaluated in parallel by default. Use .then()
to create sequential rule evaluation:
Customizing Error Messages
Override default messages using .withMessage()
:
Example: Complete Validation Setup
Validation Controllers
The ValidationController orchestrates validation execution and error rendering in your application. Controllers manage the validation lifecycle and maintain the current validation state.
Creating a Controller
You can create a controller using dependency injection:
Alternatively, use the ValidationControllerFactory:
Validation Triggers
Controllers support different validation triggers that determine when validation occurs:
blur
: Validates when an input loses focus (default)focusout
: Validates when an element or its children lose focuschange
: Validates when the model value changeschangeOrBlur
: Validates on both change and blur eventschangeOrFocusout
: Validates on both change and focusout eventsmanual
: Validation only occurs when explicitly called
Set the trigger in your view-model:
Validate and Reset Methods
Trigger validation programmatically using the controller's methods:
You can validate specific objects or properties:
Displaying Validation Errors
Built-in Error Display
The simplest way to display validation errors is using the validation-errors
attribute and controller's errors property:
Bootstrap Form Integration
Here's an example using Bootstrap form styling:
Custom Renderer Implementation
Create custom renderers for more control over error display:
Register your custom renderer with the controller:
Renderer Binding Context
When using validation-errors, you can specify both the errors and the controller:
Advanced Validation Techniques
Conditional Validation
Implement conditional validation using the .when()
method:
Custom Rules
Create reusable custom validation rules using ValidationRules.customRule()
:
Entity Validation
Validate entire objects using controller's addObject()
method:
Define object-level rules using ensureObject()
:
Integration
Working with I18N
Integrate validation messages with aurelia-i18n:
Translation files example:
API Reference
ValidationRules
Core methods for defining validation rules:
ensure(property)
Targets a property for validation
ensureObject()
Targets the entire object for validation
required()
Validates presence
email()
Validates email format
minLength(length)
Validates minimum length
maxLength(length)
Validates maximum length
matches(regex)
Validates against regular expression
satisfies(condition)
Custom validation function
satisfiesRule(name)
Uses a custom rule
when(condition)
Conditional validation
withMessage(message)
Custom error message
on(target)
Applies rules to class/object
ValidationController
Core controller methods:
validate()
Validates all registered objects/bindings
reset()
Clears all validation results
addObject(object, rules?)
Registers object for validation
removeObject(object)
Removes object from validation
addRenderer(renderer)
Adds custom error renderer
removeRenderer(renderer)
Removes error renderer
addError(message, object, propertyName?)
Adds manual error
removeError(result)
Removes specific error
ValidateResult
Properties available in validation results:
valid
boolean
Validation result
message
string
Error message
object
any
Validated object
propertyName
string
Validated property
rule
any
Rule that generated the result
Be careful when using async custom rules, which may impact form submission timing and user experience.
Last updated
Was this helpful?