Filters
Filters are normally used by form widgets to process raw input data. They may be used individually, or chained together.
Trim
var trim = new Trim,
filteredValue = trim.filter(value);
Lowercase
var lowercase = new Lowercase,
filteredValue = lowercase.filter(value);
Uppercase
var uppercase = new Uppercase,
filteredValue = uppercase.filter(value);
Propercase
var propercase = new propercase,
filteredValue = propercase.filter(value);
Chains
Chain filters together. The result of each filter will be passed to the next filter in the chain.
var chain = new Chain({filters: [
new Trim,
new Uppercase
]}),
filteredValue = chain.filter(value);
Custom Filters
Define your own filters by extending from havok/filter/Base and overriding the filter method.
require(['havok/filter/Base', 'dojo/_base/declare'],
function(FilterBase, declare){
var Dollar = declare([FilterBase], {
filter: function(value){
return '$' + value;
}
});
var dollar = new Dollar;
filteredValue = dollar.filter(value);
}
)
Filter Factory
The filter factory can be used to create instances of filters. eg:
require(['havok/get!havok/filter/factory'],
function(factory){
factory.create(['Trim', 'Lowercase']).then(function(filter){
filteredValue = filter.filter(value);
})
}
)
The filter factory uses havok/di to create and configure the filters. It accepts any valid di config.
Validators
Validators will check if data meets certain criteria and return a message if it does not.
Havok uses the mystique validator library. Which includes:
- Alpha
- Boolean
- Chain
- CreditCard
- CreditCardExpiry
- Cvv
- Date
- Equal
- Alpha
- Float
- GreaterThan
- LessThan
- HexColor
- Int
- Length
- LessThan
- LethThanEqual
- NotEqual
- NotRequired
- Password
- Regex
- Slug
- String
- And probably more ...
Mystique Validators
Use the mystique validators as described in the mystique docs. Eg
require(['mystique/Length'], function(Length){
var validator = new Length({min: 10, max: 20}),
result = validator.isValid(myValue);
})
Mystique validators can be extended and chained. See the mystique docs for more detail.
Validator Factory
The validator factory can be used to create instances of validators. eg:
require(['havok/get!havok/validator/factory'],
function(factory){
factory.create(['Alpha', {base: 'Length', params: {min: 5, max: 10}}]).then(function(validator){
result = validator.isValid(value);
})
}
)
The validator factory uses havok/di to create and configure the validators. It accepts any valid di config.