📄 validators.js
字号:
/*
* Isomorphic SmartClient
* Version 6.5 (2008-04-30)
* Copyright(c) 1998-2007 Isomorphic Software, Inc. All rights reserved.
* "SmartClient" is a trademark of Isomorphic Software, Inc.
*
* licensing@smartclient.com
*
* http://smartclient.com/license
*/
//-------------------------------// Field Validation functions//-------------------------------// Define Validators class for docs//> @class Validator// A validator describes a check that should be performed on a value the user is trying to// save.// <p>// Validators are specified for DataSource fields via the +link{attr:DataSourceField.validators}// property. Validators that need not be run on the server can also be specified for a// specific +link{class:FormItem} or +link{class:ListGridField}.// <p>// ISC supports a powerful library of +link{class:ValidatorTypes} which have identical behavior// on both the client and the server. // <p> // Beyond this, custom validators can be defined on the client and custom validation logic// added on the server. Note that the <code>regexp</code> and <code>mask</code> validator// types are very flexible and can be used to perform virtually any kind of formatting check// that doesn't involve some large external dataset.// <p>// Custom validators can be reused on the client by adding them to the global validator list,// via the +link{classMethod:Validator.addValidator()} method.//// @visibility external// @see ValidatorTypes// @treeLocation Client Reference/Forms//< //> @attr validator.type (ValidatorTypes : null : [IR])// Type of the validator.// <p>// This can be one of the built-in +link{class:ValidatorTypes}, or the string "custom" to define// a custom validator.// @visibility external//<//> @method validator.condition() // For a validator that is not one of the built-in +link{class:ValidatorTypes}, a function or// String expression to evaluate to see if this validator passes or fails.// <p>// Because the validator declaration itself is passed as a parameter to// <code>condition()</code>, you can effectively parameterize the validator. For example, to// create a validator that checks that the value is after a certain date:<pre> // { type:"custom", afterDate:new Date(), // condition:"value.getTime() > validator.afterDate.getTime()" }// </pre>// Reusable validators, like the above, can be registered as a standard validatorType by// calling +link{Validator.addValidator()}.// <P>// Note that, if a field is declared with a builtin +link{type:FieldType}, the value passed in// will already have been converted to the specified type, if possible.//// @param item (DataSourceField or FormItem) FormItem or DataSourceField on which this// validator was declared. NOTE: FormItem will not// be available during a save performed without a// form (eg programmatic save) or if the field // is not available in the form.// @param validator (Validator) Validator declaration from eg// +link{DataSourceField.validators}.// @param value (any) value to validate// @return (boolean) whether the value passed validation. True for passed, false for fail.// //// @visibility external//<//> @attr validator.errorMessage (errorMessage : null : [IR])// Text to display if the value does not pass this validation check.// <P>// If unspecified, default error messages exist for all built-in validators, and a generic// message will be used for a custom validator that is not passed.// @visibility external// @example conditionallyRequired//<//> @attr validator.stopIfFalse (boolean : false : [IR])// Normally, all validators defined for a field will be run even if one of the validators has// already failed. However, if <code>stopIfFalse</code> is set, validation will not proceed// beyond this validator if the check fails.// <P>// This is useful to prevent expensive validators from being run unnecessarily, or to allow// custom validators that don't need to be robust about handling every conceivable type of// value.// // @visibility external//<//> @attr validator.clientOnly (boolean : false : [IR])// Indicates this validator runs on the client only.// <p>// Normally, if the server is trying to run validators and finds a validator that it can't// execute, for safety reasons validation is considered to have failed. Use this flag to// explicitly mark a validator that only needs to run on the client. // // @visibility external//<// NOTE ON DEFAULT ERROR MESSAGES:// If the validator doesn't have an error message, set the defaultErrorMessage property on the// object to distinguish it from an error message set by the user (errorMessage property).// It's unnecessary to do this on the server because the error message is returned as part of// the validation result, and the validator parameters aren't modified.isc.ClassFactory.defineClass("Validator");// These need to be constants to allow the built-in validators to be i18n'd. NOTE: it would be// nice to move these definitions closer to the relevant validator, but note that some// validators have more than one error message, so we can't adopt a simple convention of naming// the errors after the validator.isc.Validator.addClassProperties({ //>@classAttr Validator.notABoolean (string : "Must be a true/false value" : [IRA]) // Default error message to display when standard <code>isBoolean</code> type validator // returns false. // @visibility external // @group i18nMessages //< notABoolean:"Must be a true/false value", //>@classAttr Validator.notAString (string : "Must be a string." : [IRA]) // Default error message to display when standard <code>isString</code> type validator // returns false. // @visibility external // @group i18nMessages //< notAString:"Must be a string.", //>@classAttr Validator.notAnInteger (string : "Must be a whole number." : [IRA]) // Default error message to display when standard <code>isInteger</code> type validator // returns false. // @visibility external // @group i18nMessages //< notAnInteger:"Must be a whole number.", //>@classAttr Validator.notADecimal (string : "Must be a valid decimal." : [IRA]) // Default error message to display when standard <code>isFloat</code> type validator // returns false. // @visibility external // @group i18nMessages //< notADecimal:"Must be a valid decimal.", //>@classAttr Validator.notADate (string : "Must be a date." : [IRA]) // Default error message to display when standard <code>isDate</code> type validator // returns false. // @visibility external // @group i18nMessages //< notADate:"Must be a date.", //>@classAttr Validator.notATime (string : "Must be a time." : [IRA]) // Default error message to display when standard <code>isTime</code> type validator // returns false. // @group i18nMessages //< notATime: "Must be a time.", //>@classAttr Validator.notAnIdentifier (string : "Identifiers must start with a letter, underscore or $ character, and may contain only letters, numbers, underscores or $ characters." : [IRA]) // Default error message to display when standard <code>isIdentifier</code> type validator // returns false. // @group i18nMessages //< notAnIdentifier: "Identifiers must start with a letter, underscore or $ character," + "and may contain only letters, numbers, underscores or $ characters.", //>@classAttr Validator.notARegex (string : "Must be a valid regular expression." : [IRA]) // Default error message to display when standard <code>isRegex</code> type validator // returns false. // @group i18nMessages //< notARegex:"Must be a valid regular expression.", //>@classAttr Validator.notAColor (string : "Must be a CSS color identifier." : [IRA]) // Default error message to display when standard <code>isColor</code> type validator // returns false. // @group i18nMessages //< notAColor:"Must be a CSS color identifier.", //>@classAttr Validator.mustBeLessThan (string : "Must be no more than ${max}" : [IRA]) // Default error message to display when standard <code>integerRange</code> type validator // returns false because the value passed in is greater than the specified maximum. // This is a dynamic string - 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 // @group i18nMessages //< mustBeLessThan:"Must be no more than ${max}", //>@classAttr Validator.mustBeGreaterThan (string : "Must be at least ${min}" : [IRA]) // Default error message to display when standard <code>integerRange</code> type validator // returns false because the value passed in is less than the specified minimum. // This is a dynamic string - 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 // @group i18nMessages //< mustBeGreaterThan:"Must be at least ${min}", //>@classAttr Validator.mustBeLaterThan (string : "Must be later than ${min}" : [IRA]) // Default error message to display when standard <code>dateRange</code> type validator // returns false because the value passed in is greater than the specified minimum date. // This is a dynamic string - 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 // @group i18nMessages //< mustBeLaterThan:"Must be later than ${min.toShortDate()}", //>@classAttr Validator.mustBeEarlierThan (string : "Must be earlier than ${max}" : [IRA]) // Default error message to display when standard <code>dateRange</code> type validator // returns false because the value passed in is less than the specified maximum date. // This is a dynamic string - 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 // @group i18nMessages //< mustBeEarlierThan:"Must be earlier than ${max.toShortDate()}", //>@classAttr Validator.mustBeShorterThan (string : "Must be less than ${max} characters" : [IRA]) // Default error message to display when standard <code>lengthRange</code> type validator // returns false becaues the value passed in has more than <code>validator.max</code> characters. // This is a dynamic string - 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 // @group i18nMessages //< mustBeShorterThan:"Must be less than ${max} characters", //>@classAttr Validator.mustBeLongerThan (string : "Must be more than ${min} characters" : [IRA]) // Default error message to display when standard <code>lengthRange</code> type validator // returns false becaues the value passed in has fewer than <code>validator.min</code> characters. // This is a dynamic string - 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 // @group i18nMessages //< mustBeLongerThan:"Must be more than ${min} characters", //>@classAttr Validator.mustBeExactLength (string : "Must be exactly ${max} characters" : [IRA]) // Default error message to display when standard <code>lengthRange</code> type validator // has <code>validator.max</code> and <code>validator.min</code> set to the same value, // and returns false because the value passed is not the same length as these limits.<br> // This is a dynamic string - 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 // @group i18nMessages //< mustBeExactLength:"Must be exactly ${max} characters", //>@classAttr Validator.notAMeasure (string : 'Must be a whole number, percentage, "*" or "auto"' : [IRA]) // Default error message to display when standard <code>isMeasure</code> type validator // returns false. // @group i18nMessages //< notAMeasure:'Must be a whole number, percentage, "*" or "auto"', //>@classAttr Validator.requiredField (string : 'Field is required' : [IRA]) // Default error message to display when validation fails for a field marked as required, // or a field with a standard <code>requiredIf</code> type validator whose condition // evaluates to true, because the field has no value. // @visibility external // @group i18nMessages //< requiredField:"Field is required", //>@classAttr Validator.notOneOf (string : 'Not a valid option' : [IRA]) // Default error message to display when standard <code>isOneOf</code> type validator
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -