Validation
This guide explains how to validate the user input for your app using the validation plugin. The plugin gives you enough flexibility to write your own rules rather than being tied to any external validation library.
Here's what you'll learn...
How to register and customize the plugin
How to define the validation rules
How to validate the data
How to apply model-based validation rules
Note If you have already used the
aurelia-validation
plugin previously and are migrating your existing Aurelia app to Aurelia vNext then jump straight to the migration guide.
Install and register the plugin
You need to install two packages to use Aurelia validation. The core validation plugin @aurelia/validation
and an adapter. Currently, only one adapter is available for validating HTML-based applications.
Installation
Register
Quick rundown
To use the validation plugin, all you have to do is inject the validation controller as well as the validation rules object to register validation rules.
Inside our HTML, we use the validate
binding behavior to signal to Aurelia that we want to validate these bindings. You might notice that both name
and age
appear in our view model above where we set some rules up.
resolve(newInstanceForScope(IValidationController))
injects a new instance of validation controller which is made available to the children ofawesome-component
. More on validation controller later.
Demo
A playable demo can be seen below if you want to see the validation plugin in action. You can try adding new properties and playing around with the code to learn how to use the validation plugin.
Last updated