📄 validators.js
字号:
// when the message is displayed, with <code>max</code> and <code>min</code> available as // variables mapped to <code>validator.max</code> and <code>validator.min</code>. // // @visibility external //< dateRange : function (item, validator, value) { if (value == null || isc.is.emptyString(value)) return true; if (!isc.isA.Date(value)) return false; var min = validator.min, max = validator.max; // make a one-time attempt to parse min and max to dates. Handy when specifying // min and max dates in XML. if (min != null && !isc.isA.Date(min)) min = validator.min = Date.parseSchemaDate(min); if (max != null && !isc.isA.Date(max)) max = validator.max = Date.parseSchemaDate(max); // Allow dynamic error messages to be eval'd, with pointers to min and max values validator.dynamicErrorMessageArguments = {validator:validator, max:max, min:min} if (isc.isA.Date(min) && value.getTime() < min.getTime()) { if (!validator.errorMessage) { validator.defaultErrorMessage = isc.Validator.mustBeLaterThan } return false; } if (isc.isA.Date(max) && value.getTime() > max.getTime()) { if (!validator.errorMessage) { validator.defaultErrorMessage = isc.Validator.mustBeEarlierThan; } return false; } return true; }, // Floats // --------------------------------------------------------------------------------------- //>@classAttr ValidatorTypes.floatLimit (validatorDefinition : object : IR) // Validate a variable as a valid floating point value, within a value range. // Range is specified via <code>validator.min</code> and <code>validator.max</code>. // Also checks precision, specified as number of decimal places in // <code>validator.precision</code>. If <code>validator.roundToPrecision</code> is set, // a value that doesn't match the specified number of decimal places will be rounded // to the nearest value that does. //< // backcompat only, replaced by floatRange and floatPrecision floatLimit : function (item, validator, value) { var roundedValue; // Check precision before max/min as rounding may push it over the edge. if (validator.precision != null) { //>!BackCompat 2005.02.03 // Old functionality always had no 'roundToPrecision' param, but always // rounded and passed. if (validator.roundToPrecision == null) validator.roundToPrecision = true; //<!BackCompat if (!isc.Validator.processValidator(item, validator, value, "floatPrecision")) return false; // from now on test with the rounded version. if (validator.resultingValue != null) value = roundedValue = validator.resultingValue; } if (validator.min != null || validator.max != null) { if (!isc.Validator.processValidator(item, validator, value, "floatRange")) { return false } else { // the second processValidator call will have cleared out resultingValue // which may have come from the precision validator. if (roundedValue != null && validator.resultingValue == null && validator.roundToPrecision) validator.resultingValue = roundedValue; } } return true; }, //>@classAttr ValidatorTypes.floatRange (validatorDefinition : object : IR) // Tests whether the value for this field is a floating point number within the range // specified. The <code>max</code> and <code>min</code> properties on the validator // are used to determine the acceptable range.<br> // Note that the <code>errorMessage</code> for this validator will be evaluated as // a dynamicString - text within <code>\${...}</code> will be evaluated as JS code // when the message is displayed, with <code>max</code> and <code>min</code> available as // variables mapped to <code>validator.max</code> and <code>validator.min</code>. // @visibility external //< floatRange : function (item, validator, value) { // skip empty fields if (value == null || isc.is.emptyString(value)) return true; // If we're passed a non numeric value, just return without adding an error. // This is appropriate since the type of the field will probably be specified as // "float" meaning that the built in float validator will also be present on the // field. var floatValue = value; if (!isc.isA.String(value)) floatValue = parseFloat(floatValue); if (isNaN(floatValue) || floatValue != value) return true; // Allow dynamic error messages to be eval'd, with pointers to min and max values validator.dynamicErrorMessageArguments = {validator:validator, max:validator.max, min:validator.min} // is the value less than the max allowable? (if specified) if (isc.isA.Number(validator.max) && floatValue > validator.max) { if (!validator.errorMessage) { validator.defaultErrorMessage = isc.Validator.mustBeLessThan; } return false; } // is the value greater than the min allowable? (if specified) if (isc.isA.Number(validator.min) && floatValue < validator.min) { if (!validator.errorMessage) { validator.defaultErrorMessage = isc.Validator.mustBeGreaterThan; } return false; } return true; }, //>@classAttr ValidatorTypes.floatPrecision (validatorDefinition : object : IR) // Tests whether the value for this field is a floating point number with the // appropriate number of decimal places - specified in <code>validator.precision</code> // If the value is of higher precision, if <code>validator.roundToPrecision</code> // is specified, the value will be rounded to the specified number of decimal places // and validation will pass, otherwise validation will fail. // @visibility external //< floatPrecision : function (item, validator, value) { // skip empty fields if (value == null || isc.is.emptyString(value)) return true; var floatValue = parseFloat(value); if (isNaN(floatValue) || floatValue != value) return false; // if validator.precision is defined, round to that precision. if (isc.isA.Number(validator.precision)) { var multiplier = Math.pow(10, validator.precision); var roundedValue = (Math.round(floatValue * multiplier))/multiplier; if (validator.roundToPrecision) { validator.resultingValue = roundedValue; return true; } else { return (floatValue == roundedValue); } return true; } } // ------------------------------------------------------------------------------------ // END of Valiator._validatorFunctions // ------------------------------------------------------------------------------------ }});isc.Validator.addClassMethods({ // Process validator is an internal method called by // DynamicForm, vauesManagers, and editable ListGrids // valuesManagers to perform validation. processValidator : function (item, validator, value, type) { // if the validator is server-side only, return true if (validator.serverOnly) return true; // if no type was specified, get it from the validator.type property if (!type) type = validator.type; var result = true; // Check whether we have a standard validator of the appropriate type. var validationFunction; if (type != null) validationFunction = this._validatorFunctions[type]; // if we didn't find a validatorFunction, use the validator.condition if one was specified if (validationFunction == null && validator.condition) { // CALLBACK API: available variables: "item,validator,value" // Convert a string callback to a function if (!isc.isA.Function(validator.condition)) { //>DEBUG this.logDebug("Creating function for validation condition:\r" + validator.condition); //<DEBUG isc.Func.replaceWithMethod(validator, "condition", "item,validator,value"); } validationFunction = validator.condition; } // if we found a validating function, call it if (validationFunction != null) { // NOTE: first clear the "resultingValue" field and suggested value of the // validator, in case the validation rule decides to set it // for Array-valued fields (field.multiple=true), validate each value in the Array if (item && item.multiple && isc.isAn.Array(value)) { var resultingValue = []; for (var i = 0; i < value.length; i++) { delete validator.resultingValue; // NOTE: don't stop on failure result = result && validationFunction(item, validator, value[i]); // capture each resulting value resultingValue[i] = (validator.resultingValue != null ? validator.resultingValue : value[i]); } // return the array value as the overall resulting value validator.resultingValue = resultingValue; } else { delete validator.resultingValue; result = validationFunction(item, validator, value); } //>DEBUG } else { this.logWarn("validator not understood on item: " + this.echo(item) + ":\r" + isc.Comm.serialize(validator)); //<DEBUG } return result; }, getErrorMessage : function (validator) { var errorMessage = validator.errorMessage; if (errorMessage == null) errorMessage = validator.defaultErrorMessage; // Convert (potentially) dynamic error message strings to straight // strings if (errorMessage && validator.dynamicErrorMessageArguments) { errorMessage = errorMessage.evalDynamicString( null, validator.dynamicErrorMessageArguments); } return errorMessage; }, //> @classMethod Validator.addValidator() (A) // Add a new validator type that can be specified as +link{Validator.type} anywhere // validators are declared, such as +link{DataSourceField.validators} or // +link{FormItem.validators}. // <br> // The <code>condition</code> argument should be a method of the same signature as // +link{Validator.condition()}. // // @param type (String) type name for the new validator // @param condition (StringMethod) function or expression to evaluate to determine whether // validation was successful // // @group validation // @visibility external // @see Validator.addValidators() //< addValidator : function (type, condition) { if (isc.isA.String(type)) { var valsObject = {}; valsObject[type] = condition; return this.addValidators(valsObject); } }, //> @classMethod Validator.addValidators() (A) // Add several new validator types at once, as though +link{addValidator()} were called // several times. // // @group validation // @param newValidators (object) Set of validators to add. This parameter should // be a JavaScript object where the property names are validator type names, and the // property values are condition functions or expressions, for example:<br> // <code>{type1:condition1, type2:condition2}</code><br>. // // @visibility external // @see Validator.addValidator() //< addValidators : function (newValidators) { for (var type in newValidators) { if (!isc.isA.Function(newValidators[type])) { isc.Func.replaceWithMethod(newValidators, type, "item,validator,value"); } } isc.addMethods(this._validatorFunctions, newValidators); } });
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -