Configuration & Customization
Aurelia's validation system is highly customizable, allowing you to configure validation behavior, replace core components, and customize how validation integrates with your application. This guide covers all available configuration options.
Overview
The validation system provides two levels of configuration:
Core Validation Configuration (
ValidationConfiguration) - Configure the core validation engineHTML Integration Configuration (
ValidationHtmlConfiguration) - Configure how validation integrates with HTML/UI
Basic Configuration
Using Default Configuration
The simplest way to register validation is with the default configuration:
import Aurelia from 'aurelia';
import { ValidationHtmlConfiguration } from '@aurelia/validation-html';
import { MyApp } from './my-app';
Aurelia
.register(ValidationHtmlConfiguration)
.app(MyApp)
.start();Customizing Configuration
Use the .customize() method to modify configuration options:
HTML Configuration Options
DefaultTrigger
Controls when validation automatically runs for inputs using the & validate binding behavior.
Type: ValidationTrigger enum
Default: ValidationTrigger.focusout
Available Triggers:
ValidationTrigger.blur- Validate when input loses focusValidationTrigger.focusout- Validate when input or any descendant loses focusValidationTrigger.change- Validate immediately on every changeValidationTrigger.changeOrBlur- Validate on change after first blurValidationTrigger.changeOrFocusout- Validate on change after first focusoutValidationTrigger.manual- Only validate when explicitly called
Example:
You can also override the trigger per-binding in your templates:
UseSubscriberCustomAttribute
Controls whether the validation-errors custom attribute is registered.
Type: boolean
Default: true
When to disable: Set to false if you don't need the validation-errors attribute (e.g., if you're using a custom error presentation strategy).
Example:
SubscriberCustomElementTemplate
Customizes the template used for the validation container custom element.
Type: string
Default:
Usage:
Template Context: The template has access to an error property with the following structure:
ValidationControllerFactoryType
Specifies the factory class used to create validation controller instances.
Type: Class<IFactory<Constructable<IValidationController>>>
Default: ValidationControllerFactory
When to customize: Create a custom factory if you need to modify how validation controllers are instantiated or add custom behavior to all controllers.
Example:
Core Validation Options
These options configure the core validation engine and can be set using ValidationConfiguration or through ValidationHtmlConfiguration.
ValidatorType
Specifies the validator implementation to use.
Type: Class<IValidator>
Default: StandardValidator
When to customize: Create a custom validator if you need to modify core validation behavior or add custom validation processing logic.
Example:
MessageProviderType
Specifies the message provider implementation for validation error messages.
Type: Class<IValidationMessageProvider>
Default: ValidationMessageProvider
When to customize: Replace the message provider to customize how validation messages are parsed, interpolated, or retrieved.
Example:
CustomMessages
Provides custom messages for validation rules, either globally or for specific rules.
Type: ICustomMessage[]
Default: []
Structure:
Example:
Messages can use interpolation:
${$displayName}- The display name of the property${$value}- The current value${$object}- The object being validated${$rule.propertyName}- Access rule properties (e.g.,${$rule.min},${$rule.max})
HydratorType
Specifies the hydrator implementation for deserializing validation rules from data (e.g., JSON from a server).
Type: Class<IValidationExpressionHydrator>
Default: ModelValidationExpressionHydrator
When to customize: Replace the hydrator if you need to support a different rule serialization format or add support for custom rule types in model-based validation.
Example:
Complete Configuration Example
Here's a comprehensive example showing multiple customization options:
Per-Component Customization
You can also customize validation on a per-component basis by manually registering validation rules with custom settings:
See Also
Registering the Plugin - Basic plugin registration
Model-Based Validation - Using the hydrator for JSON rules
Validation Controller - Working with validation controllers
Displaying Errors - Customizing error presentation
Last updated
Was this helpful?