/**
* Copyright (c) 2008, 2013, Oracle and/or its affiliates.
* All rights reserved.
*/
/**
* Constructs a RequiredValidator that ensures that the value provided is not empty
* @param {Object=} options an object literal used to provide an optional hint and error message.<p>
* @param {string=} options.hint an optional hint text. There is no default hint provided by this
* validator.
* @param {string=} options.messageSummary - an optional custom error message summarizing the
* error. When not present, the default message summary is the resource defined with the key
* <code class="prettyprint">oj-validator.required.summary</code>.<p>
* Tokens: {label} - this token can be used to substitute the label of the component at runtime. </p>
* <p>
* Example:<br/>
* "'{label}' Required"<br/>
* </p>
* @param {string=} options.messageDetail - a custom error message used for creating detail part
* of message, when the value provided is empty. When not present, the default message detail is the
* resource defined with the key <code class="prettyprint">oj-validator.required.detail</code>.
* <p>Tokens: {label} - this token can be used to substitute the label of the component at runtime.</p>
* <p>
* Example:<br/>
* "A value is required for the field '{label}'."<br/>
* </p>
*
* @export
* @constructor
* @since 0.6
*
*/
oj.RequiredValidator = function(options)
{
this.Init(options);
};
// Subclass from oj.Object or oj.Validator. It does not matter
oj.Object.createSubclass(oj.RequiredValidator, oj.Validator, "oj.RequiredValidator");
// key to access required validator specific resources in the bundle
oj.RequiredValidator._BUNDLE_KEY_DETAIL = "oj-validator.required.detail";
oj.RequiredValidator._BUNDLE_KEY_SUMMARY = "oj-validator.required.summary";
/**
* Initializes validator instance with the set options
* @param {Object=} options
* @memberof oj.RequiredValidator
* @instance
* @export
*/
oj.RequiredValidator.prototype.Init = function(options)
{
oj.RequiredValidator.superclass.Init.call(this);
this._options = options;
};
/**
* Validates value to be non-empty
*
* @param {Object|string|number} value that is being validated
* @returns {boolean} true if validation was was successful the value is non-empty
*
* @throws {Error} when fails required-ness check
* @memberof oj.RequiredValidator
* @instance
* @export
*/
oj.RequiredValidator.prototype.validate = function(value)
{
var localizedDetail, localizedSummary, detail, summary, params = {}, label = "";
// checks for empty arrays and String. Objects are considered non-null.
// Need to specifically test for if value is 0 first if number is passed on.
if ((typeof value === "number" && value === 0) || (value && value.length !== 0))
{
return true;
}
else
{
if (this._options)
{
// we have deprecated support for message param and instead use messageDetail.
detail = this._options['messageDetail'] || this._options['message'] || null;
summary = this._options['messageSummary'] || null;
label = this._options['label'] || "";
}
params = {'label': label};
localizedSummary = (summary) ? oj.Translations.applyParameters(summary, params) :
oj.Translations.getTranslatedString(this._getSummaryKey(), params);
localizedDetail = (detail) ?
oj.Translations.applyParameters(detail, params) :
oj.Translations.getTranslatedString(this._getDetailKey(), params);
throw new oj.ValidatorError(localizedSummary, localizedDetail);
}
};
/**
* A message to be used as hint, when giving a hint on the expected pattern. There is no default
* hint for this property.
*
* @returns {String|string|null} a hint message or null if no hint is available in the options
* @memberof oj.RequiredValidator
* @instance
* @export
*/
oj.RequiredValidator.prototype.getHint = function()
{
var hint = "";
if (this._options && (this._options['hint']))
{
hint = oj.Translations.getTranslatedString(this._options['hint']);
}
return hint;
};
oj.RequiredValidator.prototype._getSummaryKey = function ()
{
return oj.RequiredValidator._BUNDLE_KEY_SUMMARY;
};
oj.RequiredValidator.prototype._getDetailKey = function ()
{
return oj.RequiredValidator._BUNDLE_KEY_DETAIL;
};
Source: src/main/javascript/oracle/oj/ojvalidation/RequiredValidator.js
Oracle® JavaScript Extension Toolkit (JET)
1.1.2
E65298-01