📄 valuesmanager.js
字号:
//> @method valuesManager.processValidator() (A) // process a single validator for a field. // // @param [item] (object) Form item displaying the value. May be null if no // form item exists for the field. // @param validator (object) validation object // @param value (string) value for this field. // @param [type] (string) validator type. if not passed this is derived from // the <code>type</code> property on the validation parameter // // @return (boolean) true == passed validation, false == validation failed // @group validation //< processValidator : function (item, validator, value, type) { return isc.Validator.processValidator(item, validator, value, type); }, // _handleHiddenValidationErrors() // Internal method to display validation errors when we can't show them in a form. // This is used to handle // - errors coming from an undrawn or hidden member form // - errors coming from hidden items in a member form // - errors coming from a dataSource field for which we have no member form item. // Note these errors are all combined into a single object retrieved via this.getHiddenErrors() // if a developer needs to determine which form an error came from, they can use // getMemberForField() // Additional suppressSynch parameter - if we know the hidden errors are in synch with // the currently visible set of members / fields (IE this has been called directly from // setErrors() or validate()) we can skip the logic to ensure that this.hiddenErrors // is up to date. _handleHiddenValidationErrors : function (suppressSynch) { var errors = this.getHiddenErrors(suppressSynch); // bail if there were no errors on hidden fields if (errors == null || isc.getKeys(errors).length == 0) return; // If we have an implementation to handle the hidden validation errors, call it now. var returnVal; if (this.handleHiddenValidationErrors) { returnVal = this.handleHiddenValidationErrors(errors); } if (returnVal == false) return; // Log a warning unless this was suppressed by the handleHiddenValidationErrors method. var errorString = "Validation failed with the following errors:"; for (var fieldName in errors) { var fieldErrors = errors[fieldName]; if (!isc.isAn.Array(fieldErrors)) fieldErrors = [fieldErrors]; if (fieldErrors.length == 0) continue; errorString += "\n" + fieldName + ":"; for (var i = 0; i < fieldErrors.length; i++) { errorString += (i == 0 ? "- " : "\n - ") + fieldErrors[i]; } } this.logWarn(errorString, "validation"); }, // Validation errors APIs //> @method valuesManager.setErrors() ([A]) // Sets validation errors for this valuesManager to the specified set of errors. // Errors should be of the format:<br> // <code>{field1:errors, field2:errors}</code><br> // where each <code>errors</code> object is either a single error message string or an // array of error messages.<br> // If <code>showErrors</code> is passed in, error messages will be displayed in the // appropriate member form items. For fields with no visible form item, // +link{valuesManager.handleHiddenValidationErrors()} will be fired instead.<br> // Note that if <code>showErrors</code> is false, errors may be shown at any time via // a call to +link{ValuesManager.showErrors()}. // // @param errors (object) list of errors as an object with the field namesas keys // @param showErrors (boolean) If true display errors now. // @group errors // @visibility external //< setErrors : function (errors, showErrors) { this.clearHiddenErrors(); errors = isc.DynamicForm.formatValidationErrors(errors); var memberForms = (this.members ? this.members.duplicate() : []); for (var i = 0; i < memberForms.length; i++) { var form = memberForms[i], hiddenForm = !form.isVisible() || !form.isDrawn(), items = form.getItems(), formErrors = {}, hiddenFormErrors = {}; for (var j = 0;j < items.getLength(); j++) { var item = items[j], itemName = item.getFieldName(); if (errors[itemName] != null) { formErrors[itemName] = errors[itemName]; if (hiddenForm || !item.visible) { hiddenFormErrors[itemName] = errors[itemName]; } delete errors[itemName]; } } // suppress redraw and suppress form-level hiddenValidationError handling form.setErrors(formErrors, false); // hang onto the hidden form errors so we can fire the hiddenValidationErrors // handler. // Note: track hidden errors by form - see comments near // addHiddenErrors() / _getHiddenErrors() for more on this if (!isc.isAn.emptyObject(hiddenFormErrors)) this.addHiddenErrors(hiddenFormErrors, form); } this.addHiddenErrors(errors); // We know stored hidden errors object is up to date if (showErrors) this.showErrors(true); }, // little helper to combine errors into arrays // Returns the errors object to use _addFieldErrors : function (oldErrors, newErrors) { if (!oldErrors) return newErrors; if (!newErrors) return oldErrors; if (!isc.isAn.Array(oldErrors)) oldErrors = [oldErrors]; if (isc.isA.String(newErrors)) oldErrors.add(newErrors); else oldErrors.addList(newErrors); return oldErrors; }, //> @method ValuesManager.addFieldErrors() // Adds validation errors to the existing set of errors for the field in question. // Errors passed in should be a string (for a single error message) or an array of strings. // Pass in the showErrors parameter to immediately display the errors to the user by // redrawing the appropriate member form item (or if no visible item is found for the field // firing +link{valuesManager.handleHiddenValidationErrors()}. // @param fieldName (string) name of field to apply errors to // @param errors (string | array of strings) error messages for the field // @param showErrors (boolean) should the error(s) be immediately displayed to the user? // @group errors // @visibility external //< addFieldErrors : function (fieldName, errors, showErrors) { var hidden = true; var form = this.getMemberForField(fieldName); if (form != null) { form.addFieldErrors(fieldName, errors, false); var item = form.getItem(); if (form.isVisible() && form.isDrawn() && item && item.visible) { hidden = false; } } if (hidden) { if (!this.hiddenErrors) this.hiddenErrors = {}; var formName = form ? form.getID() : this.getID(); if (!this.hiddenErrors[formName]) this.hiddenErrors[formName] = {}; this.hiddenErrors[formName][fieldName] = this._addFieldErrors(this.hiddenErrors[formName][fieldName], errors); } if (showErrors) this.showFieldErrors(fieldName); }, //> @method ValuesManager.setFieldErrors() // Sets validation errors for some field in the valuesManager.<br> // Errors passed in should be a string (for a single error message) or an array of strings. // Pass in the showErrors parameter to immediately display the errors to the user by // redrawing the appropriate member form item (or if no visible item is found for the field // firing +link{valuesManager.handleHiddenValidationErrors()}. // @param fieldName (string) name of field to apply errors to // @param errors (string | array of strings) error messages for the field // @param showErrors (boolean) should the error(s) be immediately displayed to the user? // @group errors // @visibility external //< setFieldErrors : function (fieldName, errors, showErrors) { var hidden = true; var form = this.getMemberForField(fieldName); if (form != null) { form.setFieldErrors(fieldName, errors, false); var item = form.getItem(); if (form.isVisible() && form.isDrawn() && item && item.visible) { hidden = false; } } if (hidden) { if (!this.hiddenErrors) this.hiddenErrors = {}; this.hiddenErrors[fieldName] = errors; } if (showErrors) this.showFieldErrors(fieldName); }, //> @method valuesManager.clearErrors() ([]) // Clears all errors from member forms. // @param showErrors (boolean) If true, clear any visible error messages. // @group errors // @visibility external //< clearErrors : function (showErrors) { this.setErrors({}, showErrors); }, //> @method valuesManager.clearFieldErrors() // Clear all validation errors associated with some field in this form // @param fieldName (string) field for which errors should be cleared // @param show (boolean) if true, and the field is present in one of our member forms, // redraw it to clear any currently visible validation errors // @group errors // @visibility external //< clearFieldErrors : function (fieldName, show) { var form = this.getMemberForField(fieldName); if (form) form.clearFieldErrors(fieldName, show); if (this.hiddenErrors) delete this.hiddenErrors[fieldName]; }, //> @method valuesManager.getErrors() // Returns the set of errors for this valuesManager. // Errors will be returned as an object of the format <br> // <code>{field1:errors, field2:errors}</code><br> // Where each errors object is either a single error message or an array of error message // strings. // @return (object) Object contining mapping from field names to error strings. Returns null // if there are no errors for this valuesManager. // @group errors // @visibility external //< // Stored errors include those stored as "hiddenErrors", with no associated form (came from // a datasource field definition only, presumably), and errors from member forms getErrors : function () { // pick up stored hidden errors. // [don't bother to synch - we're not interested in whether they're truly hidden or not now] var errors = isc.addProperties({}, this.getHiddenErrors(true)); // add errors from each member form if (this.members) { for (var i = 0; i < this.members.length; i++) { isc.addProperties(errors, this.members[i].getErrors()); } } if (!isc.isA.emptyObject(errors)) return errors return null }, //> @method valuesManager.getFieldErrors() // Returns any validation errors for some field in this valuesManager. // Errors will be returned as either a string (a single error message), or an array // of strings. If no errors are present, will return null. // @param fieldName (string) fieldName to check for errors // @return (string | array of strings) error messages for the field passed in // @group errors // @visibility external //< getFieldErrors : function (fieldName) { var form = this.getMemberForField(fieldName) if (form) return form.getFieldErrors(fieldName); if (this.hiddenErrors && this.hiddenErrors[this.getID()]) return this.hiddenErrors[this.getID()][fieldName]; }, //> @method valuesManager.hasErrors() // Are there any errors associated with any fields in this valuesManager? // @return (boolean) returns true if there are any oustanding validation errors, false // otherwise. // @group errors // @visibility external //< hasErrors : function () { if (this.hiddenErrors && !isc.isA.emptyObject(this.hiddenErrors)) { for (var form in this.hiddenErrors) { if (this.hiddenErrors[form] && !isc.isAn.emptyObject(this.hiddenErrors[form])) return true; } } if (this.members == null) return false; for (var i = 0; i < this.members.length; i++) { if (this.members[i].hasErrors()) return true; } return false; }, //> @method valuesManager.hasFieldErrors() // Are there any errors associated with a field in this valuesManager? // @param fieldName (string) field to check for errors // @return (boolean) returns true if there are any oustanding validation errors, false // otherwise. // @group errors // @visibility external //<
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -