Class: Validation

Oracle® JavaScript Extension Toolkit (JET)
5.0.0

E90577-01

QuickNav

Validation

Version:
  • 5.0.0
Since:
  • 0.6
Module:
  • ojvalidation-base
Used to register and retrieve converterFactories and validationFactories. There is no need to call the constructor.
See:

Methods

(static) converterFactory(type, instance)

Method to register and retrieve converter factory instances by name. When passed only the name, an existing factory (registered for the name) is returned. Callers can expect to get back the default 'number', 'datetime', or 'color' converters. When passed two arguments, a new factory for the name is registered. If the name already exists the new instance replaces the old one.
Parameters:
Name Type Argument Description
type string a case insensitive name of the converter factory. e.g., 'number' (or oj.ConverterFactory.CONVERTER_TYPE_NUMBER), 'datetime' (or oj.ConverterFactory.CONVERTER_TYPE_DATETIME), or 'color' (or oj.ConverterFactory.CONVERTER_TYPE_COLOR).
instance Object <optional>
the instance of the factory that implements the contract for oj.ConverterFactory.
See:
Examples

Initialize a JET datetime converter instance using options and use it in a knockout-binding and bind it to the converter attribute on the JET form element.

----- Javascript ViewModel----
self.converter = ko.observable(
    oj.Validation.converterFactory(oj.ConverterFactory.CONVERTER_TYPE_DATETIME).
      createConverter({formatType: 'date', dateFormat: 'full'}));
----- HTML -----
<oj-input-date value="{{endDate}}" converter="[[converter]]"></oj-input-date>

Create your own converter, register it, and use it when displaying information on the page to the user.

----- Javascript -----
RelativeDateTimeConverterFactory = (function () {
 function _createRelativeDateTimeConverter(options)
 {
   // this is a custom converter (code not shown here) that formats the datetime into "Today" or 
   // "Tomorrow", or "This Week"/"Next Week", etc.
   return new RelativeDateTimeConverter(options);
 }
 return {
   'createConverter': function (options) {
     return _createRelativeDateTimeConverter(options);
   }
 };
 }());
oj.Validation.converterFactory("relativeDate", RelativeDateTimeConverterFactory);
 // Use our custom relativeDate converter.
 // In this demo, we want to see in the Schedule For column the words 
 // Today or Tomorrow so we set the relativeField option's value
 // to 'day'. If we want to see This Week, we'd set it to 'week', etc.
 var rdConverter =  oj.Validation.converterFactory("relativeDate")
 .createConverter({relativeField: 'day', year: "numeric", month: "numeric", day: "numeric"});
 ...
 // Our custom converter's format function returns an object with 'value' and 'title'.
 // We put the 'value' in innerHTML so the user can read it. E.g., Today or Tomorrow.
 // And we put the actual date in the title. The user can read it when they hover over
 // the word Today or Tomorrow.
 content = rdConverter.format(context.row.ScheduleFor);
 span.setAttribute('title', content.title);
 span.innerHTML = content.value;
...

On your JET form component that has a 'converter' attribute, set the 'converter' attribute using "type" of a registered converter. This could be a JET Converter or a oj.Converter (or Object that duck-types it) that you wrote. (it must implement parse and format since our JET form components call format and parse.

<oj-input-date value="{{date}}" 
converter= '{"type":"datetime", 
"options": {"year": "numeric", "month": "long", "day": "numeric"}}'> </oj-input-date>

(static) getDefaultConverterFactory(type) → {Object|null}

Returns the default converter factory instances for the supported types as defined by the oj.ConverterFactory. Instead of calling this method, it should be sufficient to use oj.Validation.converterFactory.
Parameters:
Name Type Description
type string The default converter factory for the type. Supported types are 'number' and 'datetime'
See:
Returns:
an instance of oj.ConverterFactory or null if an unknown type is requested.
Type
Object | null

(static) getDefaultValidatorFactory(type) → {Object|null}

Returns the default validator factory instance for the requested types as defined by the oj.ValidatorFactory. Instead of calling this method, it should be sufficient to use oj.Validation.validatorFactory.
Parameters:
Name Type Description
type string The default converter factory for the type. Supported types are 'number' and 'datetime'
See:
Returns:
an instance of oj.ConverterFactory or null if an unknown type is requested.
Type
Object | null

(static) validatorFactory(type, instance)

Method to register and retrieve validator factory instances by name. When passed only the name, an existing factory (registered for the name) is returned. When passed two arguments, a new factory for the name is registered. If the name already exists the new instance replaces the old one.
Parameters:
Name Type Argument Description
type string a case insensitive name of the validator factory.
instance Object <optional>
the instance of the factory that implements the contract for oj.ValidatorFactory.
Since:
  • 0.6
See:
Examples

Initialize a JET validator instance using options and use it in a knockout-binding and bind it to the validators attribute on the JET form element.

----- Javascript ViewModel----
var options = {min: 5, max: 10};
var validator =
oj.Validation.validatorFactory(
oj.ValidatorFactory.VALIDATOR_TYPE_LENGTH).createValidator(options);
self.validator = ko.observable([validator]);
----- HTML -----
<oj-input-text validators="[[validator]]"></oj-input-text>

An easier usage is to return an Object since the validators option takes an array where each item is either an instance that duck types oj.Validator, or is an Object literal containing type and options.

----- Javascript ViewModel -----
self.validators = ko.computed(function() {
return [{type: 'numberRange', options: { min: 10000.05, max: 25000.95, 
hint: {inRange: 'Enter a value between {min} and {max}.'}}}];
});
----- HTML -----
<oj-input-text id="numberRange1" value="{{numberValue1}}" validators="[[validators]]">
</oj-input-text>

Get a registered validator with your options, and calls its validate method on your own value.

var reqValOptions = {
  'hint': reqTrans['hint'] || null,
  'label': this._getLabelText(), 
  'messageSummary': reqTrans['messageSummary'] || null,
  'messageDetail': reqTrans['messageDetail'] || null
};
var vf = oj.Validation.validatorFactory(oj.ValidatorFactory.VALIDATOR_TYPE_REQUIRED);
var requiredValidator = vf.createValidator(reqValOptions);
var isValid = validator.validate(value);

Type Definitions

FactoryRegisteredValidatorOrConverter

A ducktype for accessing converter or validator created and/ registered by referring to oj.Validation.validatorFactory or oj.Validation.converterFactory
Properties:
Name Type Argument Description
type string the name of the factory registered
options Object <optional>
options to the validator or converter