Class: Validator

Oracle® JavaScript Extension Toolkit (JET)
4.2.0

E91398-01

QuickNav

oj. Validator

Version:
  • 4.2.0
Since:
  • 0.6

Constructor

new Validator()

Examples

Create a Validator and implement its methods.

 var TimeIncrementValidator = function () { };
 //Need to be a subclass of oj.Validator
 oj.Object.createSubclass(TimeIncrementValidator, oj.Validator, "TimeIncrementValidator");
 //Below validate function will perform the necessary validation
 TimeIncrementValidator.prototype.validate = function (value)
 {
   if (!value)
   {
     return value;
   }
   value = oj.IntlConverterUtils.isoToLocalDate(value);
   var minutes = value.getMinutes();
   //Check if the minute is in increment of 15 by taking a modulo
   if ((minutes % 15) !== 0)
   {
     throw new oj.ValidatorError("Uh oh, not an increment value.", "Hey put the increment value not " + value);
   }
   else 
   {
     return value;
   }
 };
 
 //No hint for this validator
 TimeIncrementValidator.prototype.getHint = function ()
 {
   return null;
 };

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() → {*}

Returns a hint that describes the validator rule.
Returns:
a hint string or null
Type
*
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>

Init()

Initializes validator instance with the set options

validate(value) → {*}

Validates the value.
Parameters:
Name Type Description
value Object to be validated
Throws:
Error if validation fails
Returns:
a boolean true if validation passes.
Type
*
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>