Validators
Contents |
This article provides information on how to use validators.
Main Principle
Aria Templates is based on a pattern that clearly separates the model and the view, and as such, guaranteeing the validity and completeness of something that the user has entered is done at model level directly, not at view level.
In Aria Templates, this means that the module controller (in charge of the model) has this responsibility.
Data Model Validators
Validator Classes
The framework provides the following set of validator helper classes that can be "attached" to your data model in order to validate it:
| Classpath | Type to validate | Usage |
|---|---|---|
| aria.utils.validators.Alpha | String
|
Validates that the value is a string composed only of letters |
| aria.utils.validators.AlphaInternational | String
|
Validates that the value is a string composed only of letters or accentuated letters |
| aria.utils.validators.AlphaNum | String
|
Validates that the value is a string composed only of letters and numbers |
| aria.utils.validators.Email | String
|
Validates that the value is a string matching an email pattern |
| aria.utils.validators.Mandatory | Any
|
Validates that the value is not null, not false, not an empty string |
| aria.utils.validators.MinMaxLength | String
|
Validates that the value is a string and that its size is between the min and max size given. |
| aria.utils.validators.Number | String
|
Validates that the value is a string matching a number pattern |
| aria.utils.validators.Object | Object
|
Validates that the object is a valid object type |
| aria.utils.validators.Phone | String
|
Validates that the value is a string matching a phone pattern |
| aria.utils.validators.RegExp | String
|
Validates that the value is a string and match given regular expression. |
| aria.utils.validators.String | String
|
Validates that the value is a string without special characters |
These validators should be loaded as dependencies into your module controller class:
File /utils/validators/Validators.js
'aria.utils.validators.Email',
'aria.utils.validators.Phone',
'aria.utils.validators.Alpha',
'aria.utils.validators.AlphaInternational',
'aria.utils.validators.AlphaNum',
'aria.utils.validators.MultipleValidator'
These classes implement the interface aria.utils.validators.IValidator that defines one validate method only. This means that any number of extra validator classes can be created if needed.
The validate function returns a specific object, depending on the success or not of the validation. _validationFailed and _validationSucceeded from aria.utils.validators.Validator base class.
MultipleValidator
The MultipleValidator is a special validator that allows you to queue several validators on the same piece of datamodel. By default, it will execute all validators added to it, but have two special behaviours :
- If
breakOnMessageproperty is set to true, it will stop on the first message raised by a contained validator. - If the validator is created with a custom message OR
groupMessagesis set to true, messages from contained validators will be added as sub messages.
Data Model Validation Utility
On top of these validators, the Aria Templates aria.utils.Data singleton can be used to manage the association of validators to the data model. These methods are described here.
Module Controller Code Example
Here is a module controller example using validators
If we were to use a data model such as the following:
File /utils/validators/Validators.js
sampleType : "ard",
firstName : "",
lastName : "",
phoneNumber : "",
email : "",
errorMessages : []
In the module controller, we could set the appropriate validators like this:
File /utils/validators/Validators.js
aria.utils.Data.setValidator(this._data, "firstName", new aria.utils.validators.AlphaInternational());
aria.utils.Data.setValidator(this._data, "lastName", new aria.utils.validators.AlphaInternational());
aria.utils.Data.setValidator(this._data, "email", new aria.utils.validators.Email());
Now, let's assume one of the templates associated to this module controller calls its submit method when a button is clicked. The submit method will validate the model and then make a request to process the messages:
File /utils/validators/Validators.js
// Prepare a message holder for the list messages that might be returned when validating the model
var messages = {};
// Validate the whole model here, passing the message holder
aria.utils.Data.validateModel(this._data, messages);
// Inject the list of messages that were returned from the validateModel method call in the data model
// (to show errors in screen)
this.json.setValue(this._data, "errorMessages", messages.listOfMessages);
// Check the nbrOfE field on the message holder to see if validation succeeded or not
if (!(messages.nbrOfE > 0)) {
// If it did, proceed with the request to the server
this.submitJsonRequest("submit", this.json.copy(this._data), "_onServerResponse");
}
Validators and Events
As previously mentioned, Validators are set on the data model and are executed when the validateModel/validateValue method is called. It is also possible to configure when a validator can be executed by declaring it in the validators eventToValidate property. By default the value of the property is set to "onsubmit" and the property is only currently supported for "onsubmit" and "onblur".
Using Single Validators
In the module controller a validators eventToValidate property can be set using the setValidator method:
File /utils/validators/Validators.js
In this example we don't pass groups, in fact it doesn't matter if you grouped the validators or not, as groupings are not applied for onblur validation.
For more details on validator groupings see below.
Using Multiple Validators
When using multiple validators without passing an event parameter then the original syntax will suffice:
File /utils/validators/Validators.js
myMultipleValidator.add(new aria.utils.validators.AlphaInternational())
When passing an event parameter the following new API is used:
File /utils/validators/Validators.js
validator : new aria.utils.validators.Mandatory("The first name is a required field using a mandatory validator."),
event : "onblur"
Both techniques can be mixed:
File /utils/validators/Validators.js
{
validator : new aria.utils.validators.Mandatory("The first name is a required field using a mandatory validator."),
event : "onblur"
},
new aria.utils.validators.AlphaInternational()
Example of Validators and Events
Validators and Groups
As well as configuring validators to be executed on an event it is also possible to group validators within the data model. Each validator that extends the super class Validator will have a native property called validationGroups. This array is used to store the groups that a validator is a member of. A validator can be a member of multiple groups irrespective of the values containing the validators that are being validated.
Using Single Validators
To set a validator as a member of a group a new groups parameter needs to be passed to the utility method setValidator within the Data utility:
File /utils/validators/Validators.js
The new groups parameters for setting a validators groups and for validating a value or the entire model are optional. Here is an example of validating a value which has validators that are a part of the "mandatory" group:
File /utils/validators/Validators.js
Using Multiple Validators
It is possible to set groups on multiple validators using the two following approaches:
File /utils/validators/Validators.js
aria.utils.Data.setValidator(this._data, "firstName", myMultipleValidator, ["group1"]);
or
File /utils/validators/Validators.js
when using the add method the original syntax is still supported:
File /utils/validators/Validators.js
now groups are supported through the following API:
File /utils/validators/Validators.js
validator : new aria.utils.validators.Mandatory("The first name is a required field using a mandatory validator."),
groups : ["group1"]
both techniques can be mixed:
File /utils/validators/Validators.js
myMultipleValidator.add(
{
validator : new aria.utils.validators.Mandatory("The first name is a required field using a mandatory validator."),
groups : ["group1"]
},
new aria.utils.validators.AlphaInternational()
