Constructor
(abstract) new Validator(options)
Parameters:
| Name | Type | Argument | Description |
|---|---|---|---|
options |
Object |
<optional> |
An object which contains the options for the validator |
Examples
Create a Validator and implement its methods.
//Validator to ensure that the selected time is a multiple of 15 minute interval.
//This converter takes in a converter option which will be used to format the hint/error
//message shown to the user. It is mandatory to pass the converter option.
var TimeIncrementValidator = function (options) {
if(options && options.converter){
this._converter = options.converter;
}
};
//Need to be a subclass of oj.Validator
oj.Object.createSubclass(TimeIncrementValidator, oj.Validator, "TimeIncrementValidator");
// Validates if the passed in value is a multiple of 15 minute interval.
// Throws an error if the validation fails.
TimeIncrementValidator.prototype.validate = function (value)
{
if (value)
{
var currentTime = oj.IntlConverterUtils.isoToLocalDate(value);
var previousValidValue, nextValidValue, sampleMinutes;
var minutes = currentTime.getMinutes();
//Check if the minute is in increment of 15 by taking a modulo
if ((minutes % 15) !== 0)
{
sampleMinute = Math.floor((minutes / 15))*15;
currentTime.setMinutes(sampleMinute);
previousValidValue = oj.IntlConverterUtils.dateToLocalIso(currentTime);
sampleMinute = sampleMinute+ 15;
if(sampleMinute >= 60){
sampleMinute = 0;
currentTime.setTime(currentTime.getTime() + (60*60*1000));
}
currentTime.setMinutes(sampleMinute);
nextValidValue = oj.IntlConverterUtils.dateToLocalIso(currentTime);
throw new oj.ValidatorError("Only multiples of 15 minute intervals are allowed.", "For example, " + this._converter.format(previousValidValue) +
" or "+ this._converter.format(nextValidValue));
}
}
};
//Generates a hint message with 4 different samples of valid values.
TimeIncrementValidator.prototype.getHint = function ()
{
var currentTime = new Date();
var hintMessage = "Only 15 minute intervals accepted, for example";
//generate 4 sample values
for (var i = 0; i < 4; i++) {
currentTime.setMinutes(i * 15);
hintMessage = hintMessage + ", " + this._converter.format(oj.IntlConverterUtils.dateToLocalIso(currentTime));
}
return hintMessage;
};
Create a Validator and implement its methods. Bind it to the JET form component which will call this 'validate' to validate the user's input.
// Validator that ensures endDate is never less than start date
self.endDateValidator = {
'validate' : function(value)
{
var value = oj.IntlConverterUtils.isoToLocalDate(value);
var start = oj.IntlConverterUtils.isoToLocalDate(self.startDate());
if (value && !(value.getFullYear() > start.getFullYear() ||
{
throw new oj.ValidatorError('End Date cannot be less than Start Date');
}
},
'getHint' : function() { return "End Date needs to be greater than Start Date");}
};
-- HTML --
<oj-input-date id="nextday" value="{{endDate}}"
validators="{{[weekendDateValidator, endDateValidator]}}"></oj-input-date>
Methods
-
getHint() → {string|null}
-
Returns a hint that describes the validator rule.
Returns:
a hint string or null- Type
- string | null
Example
Create a Validator and implement the getHint method. Bind it to the JET form component which will show the hint.
// Validator that ensures endDate is never less than start date self.endDateValidator = { 'validate' : function(value) { ... }, 'getHint' : function() { return "End Date needs to be greater than Start Date");} }; -- HTML -- <oj-input-date id="nextday" value="{{endDate}}" validators="{{[weekendDateValidator, endDateValidator]}}"></oj-input-date> -
validate(value) → {any}
-
Validates the value. The function typically returns true if the validation passes and throws an error if it fails. However some of the implementations (like LengthValidator) returns the original input if the validation is successful.
Parameters:
Name Type Description valueany to be validated Throws:
if validation fails- Type
- Error
Returns:
a boolean true or the original value if validation passes.- Type
- any
Example
Create a Validator and implement the validate method. Bind it to the JET form component which will call this 'validate' to validate the user's input.
// Validator that ensures endDate is never less than start date self.endDateValidator = { 'validate' : function(value) { var value = oj.IntlConverterUtils.isoToLocalDate(value); var start = oj.IntlConverterUtils.isoToLocalDate(self.startDate()); if (value && !(value.getFullYear() > start.getFullYear() || { throw new oj.ValidatorError('End Date cannot be less than Start Date'); } }, 'getHint' : function() { return "End Date needs to be greater than Start Date");} }; -- HTML -- <oj-input-date id="nextday" value="{{endDate}}" validators="{{[weekendDateValidator, endDateValidator]}}"></oj-input-date>