📄 validators.js
字号:
// is not passed. // @visibility external // @group i18nMessages //< notOneOf:"Not a valid option", //>@classAttr Validator.notAFunction (string : 'Must be a function.' : [IRA]) // Default error message to display when standard <code>isFunction</code> type validator // returns false. // @group i18nMessages //< notAFunction:'Must be a function.', _$true : "true", _$false : "false", // Actually store the standard validators on Validator._validatorFunctions _validatorFunctions : { // Create "Validators" as a pseudo class for JSDocs - documents the // set of available validator types. //> @class ValidatorTypes // The set of standard validator types available on all fields.<br> // To make use of some standard validator type for a field in a DataSource, or // DynamicForm instance, specify the <code>validators</code> property to an array // containing a validator definition where the <code>type</code> property is set to // the appropriate type. // A custom error message can be specified for any validator type by setting the // <code>errorMessage</code> property on the validator definition object, and some // validator types make use of additional properties on the validator definition // object such as <code>max</code> or <code>min</code>.<br> // For example, to make use of the <code>integerRange</code> validator type:<br><br><code> // field:{<br> // validators:[<br> // {type:"integerRange", min:1, max:100}<br> // ]<br> // } // </code><br><br> // // @visibility external // @treeLocation Client Reference/Forms/Validator //< // isType validators // ------------------------------------------------------------------------------------ //>@classAttr ValidatorTypes.isBoolean (validatorDefinition : object : IR) // Validation will fail if this field is non-empty and has a non-boolean value. // @visibility external //< isBoolean : function (item, validator, value) { // skip empty fields if (value == null || isc.is.emptyString(value)) return true; if (isc.isA.Boolean(value)) return true; if (!validator.errorMessage) { validator.defaultErrorMessage = isc.Validator.notABoolean; } if (isc.isA.String(value)) { var Validator = isc.Validator; validator.resultingValue = (value == Validator._$true); // "true" and "false" is the valid String representation of a boolean return (value == Validator._$true || value == Validator._$false); } else if (isc.isA.Number(value)) { validator.resultingValue = (value != 0); // 0 and 1 is the valid numeric representation of a boolean return (value == 0 || value == 1); } // anything else is a failure, but we still tell you it's boolean value validator.resultingValue = !!value; return false; }, //>@classAttr ValidatorTypes.isString (validatorDefinition : object : IR) // Validation will fail if the value is not a string value. // @visibility external //< isString : function (item, validator, value) { if (value == null || isc.isA.String(value)) return true; if (!validator.errorMessage) validator.defaultErrorMessage = isc.Validator.notAString; validator.resultingValue = isc.iscToLocaleString(value); return true; }, //>@classAttr ValidatorTypes.isInteger (validatorDefinition : object : IR) // Tests whether the value for this field is a whole number. If // <code>validator.convertToInteger</code> is true, float values will be converted // into integers and validation will succeed. // @visibility external //< isInteger : function (item, validator, value) { // skip empty fields if (value == null || isc.is.emptyString(value)) return true; if (!validator.errorMessage) validator.defaultErrorMessage = isc.Validator.notAnInteger; // if the value can't be resolved to a number, return false if (isNaN(value)) return false; //Not a number or a string that resolves to a number. // Note: this routine will be subject to JavaScript's rounding errors for extremely // large numbers (16+ digits) var intValue = parseInt(value), isInteger = (value == intValue); if (validator.convertToInteger) { // Parse as float and round instead of parseInt() because parseInt() is // basically Math.floor(). We want 1.5 to become 2, etc. var floatValue = parseFloat(value), intValue = Math.round(floatValue); // reset suggested value (no change if already an integer) validator.resultingValue = intValue; // return true - if we're doing the conversion allow validation to succeed return true; } else { // If we were passed an integer, still update the resulting value - this // will ensure that 1.0 is stored as just 1. if (isInteger) { validator.resultingValue = intValue; return true; } else return false; } }, //>@classAttr ValidatorTypes.isFloat (validatorDefinition : object : IR) // Tests whether the value for this field is a valid floating point number. // @visibility external //< isFloat : function (item, validator, value) { if (value == null || isc.is.emptyString(value)) return true; if (!validator.errorMessage) validator.defaultErrorMessage = isc.Validator.notADecimal; // is the value a valid float? var floatValue = parseFloat(value); if (isNaN(floatValue) || floatValue != value) return false; validator.resultingValue = floatValue; return true; }, isDate : function (item, validator, value) { if (value == null || isc.is.emptyString(value) || isc.isA.Date(value)) return true; if (!validator.errorMessage) validator.defaultErrorMessage = isc.Validator.notADate; var dateValue = Date.parseSchemaDate(value); if (dateValue == null) return false; validator.resultingValue = dateValue; return true; }, // Not exposed, as developer will likely want to customize acceptable time format/ // parsing function. isTime : function (item, validator, value) { if (value == null || isc.is.emptyString(value) || isc.isA.Date(value)) return true; if (isc.Time.parseInput(value, true) != null) return true; if (!validator.errorMessage) validator.defaultErrorMessage = isc.Validator.notATime; return false; }, // This is used for validating ISC components defined in XML // Leave as un-exposed for now. isIdentifier : function (item, validator, value) { if (value == null || isc.is.emptyString(value)) return true; if (!validator.errorMessage) { validator.defaultErrorMessage = isc.Validator.notAnIdentifier; } return value.match(/^[a-zA-Z_\$][\w\$]*$/) != null; }, // This is used for validating ISC components defined in XML // Leave as un-exposed for now. isRegexp : function (item, validator, value) { if (value == null || isc.is.emptyString(value)) return true; if (!validator.errorMessage) validator.defaultErrorMessage = isc.Validator.notARegex; if (typeof value == 'object' && value.constructor == RegExp) return true; if (isc.Browser.isDOM) { if (!isc.Validators._isRegexp) { isc.Validators._isRegexp = new Function("value", "try{var regex=new RegExp(value)}catch(e){return false}return true"); } return isc.Validators._isRegexp(value); } else { var regex = new RegExp(value); return true; } }, //>@classAttr ValidatorTypes.isFunction (validatorDefinition : object : IR) // Tests whether the value for this field is a valid expression or function; if it is // valid, creates a StringMethod object with the value, and set the resultingValue to // the StringMethod //< isFunction : function (item, validator, value) { if (value == null || isc.is.emptyString(value) || value == isc.Class.NO_OP || isc.isA.StringMethod(value)) { return true; } if (!validator.errorMessage) validator.defaultErrorMessage = isc.Validator.notAFunction; try { isc.Func.expressionToFunction("", value); } catch (e) { return false; } // catch the case where we have a function derived from an Action // in this case pick up the original action again. if (value.iscAction) value = value.iscAction; validator.resultingValue = isc.StringMethod.create({value:value}); return true; }, // isColor() - used for validating ISC components defined in XML // Leave as un-exposed for now. isColor : function (item, validator, value) { if (!validator.errorMessage) validator.defaultErrorMessage = isc.Validator.notAColor; // empty string/undefined/null is generally treated as the transparent color, so allow // that. If an actual entry is required, you can specify the 'required' validator if (!value) return true; return isc.isA.color(value); }, // This is used for validating ISC components defined in XML // Leave as un-exposed for now. isMeasure : function (item, validator, value) { if (value == null || isc.is.emptyString(value) || value == "*") return true; if (!validator.errorMessage) validator.defaultErrorMessage = isc.Validator.notAMeasure; // if it ends in percent, check if it's all digits if (isc.isA.String(value) && value.charAt(value.length - 1) == '%') { value = value.slice(0, -1); // Not using parseInt here because parseInt returns a valid number if the // string is prefixed with a valid number return value.match(/\d+\.?\d*/) != null; } return isc.Validator.processValidator(item, validator, value, "integerOrAuto"); }, // This is used for validating ISC components defined in XML // Leave as un-exposed for now. integerOrAuto : function (item, validator, value) { if (value == null || isc.is.emptyString(value) || (isc.isA.String(value) && value.toLowerCase() == "auto")) return true; return isc.Validator.processValidator(item, validator, value, "isInteger"); }, // Generic (typeless) validators // --------------------------------------------------------------------------------------- //>@classAttr ValidatorTypes.requiredIf (validatorDefinition : object : IR) // RequiredIf type validators should be specified with an <code>expression</code> // property set to a stringMethod, which takes three parameters:<ul> // <li>item - the DynamicForm item on which the error occurred (may be null) // <li>validator - a pointer to the validator object // <li>value - the value of the field in question</ul> // When validation is perfomed, the expression will be evaluated - if it returns // <code>true</code>, the field will be treated as a required field, so validation // will fail if the field has no value. // @visibility external // @example conditionallyRequired //< requiredIf : function (item, validator, value) { // CALLBACK API: available variables: "item,validator,value" // Convert a string callback to a function if (validator.expression != null && !isc.isA.Function(validator.expression)) { isc.Func.replaceWithMethod(validator, "expression", "item,validator,value"); } var required = validator.expression(item,validator,value); // Default to displaying the 'requiredField' error message. if (validator.errorMessage == null) validator.errorMessage = isc.Validator.requiredField; // if the item is not required, or isn't empty, return true
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -