📄 form-field.js
字号:
} el.zpFormField.validate(); } } if(!ev){ ev = window.event; } this.fireEvent("valueChanged", ev); this.fireEvent("booleanValueChanged", ev); this.fireEvent("all", ev, "booleanValueChanged"); this.form.fireEvent("valueChanged", ev); this.form.fireEvent("booleanValueChanged", ev); this.form.fireEvent("all", ev, "booleanValueChanged"); return true;};/** * @private * Handler for keydown event. */Zapatec.Form.Field.prototype.keydown = function(evt){ if(!this.isEditing){ return false; } if(!evt){ evt = window.event; } this.fireEvent("keydown", evt); this.fireEvent("all", evt, "keydown"); this.form.fireEvent("keydown", evt); this.form.fireEvent("all", evt, "keydown"); this.state.lastSelectionStart = this.getSelectionStart(); this.state.lastSelectionEnd = this.getSelectionEnd(); // this is IE workaround - IE catches nonalphanumeric keys only on keydown. if( Zapatec.is_ie && ( this.hasFeature('zpFormAllowedChars') || this.hasFeature("zpFormMask") ) ){ var tmpArr = Zapatec.Utils.getCharFromEvent(evt); var charCode = tmpArr.charCode; var newChar = tmpArr.chr; if( Zapatec.Form.Utils.isSpecialKey(charCode, newChar) || this.processFunctionalKeys(evt) == true ){ return true; } if(this.hasFeature("zpFormMask")){ if(this.processCustomKeys(charCode) == true){ return true; } return false; } if(this.hasFeature('zpFormAllowedChars')){ this.setValueFromField(); } } if(this.dropDown){ this.dropDown.hide(); } return true;};/** * @private * Handler for keypress event. * @param {Event} event object */Zapatec.Form.Field.prototype.keypress = function(evt) { if(!this.isEditing){ return false; } if(!evt){ evt = window.event; } this.fireEvent("keypress", evt); this.fireEvent("all", evt, "keypress"); this.form.fireEvent("keypress", evt); this.form.fireEvent("all", evt, "keypress"); if (this.hasFeature('zpFormAllowedChars')){ if(this.processFunctionalKeys(evt) == true){ return true; } //the key that was pressed var tmpArr = Zapatec.Utils.getCharFromEvent(evt) var charCode = tmpArr.charCode; var newChar = tmpArr.chr; if( ( Zapatec.Form.Utils.isSpecialKey(charCode, newChar) || charCode == 8 || charCode == 46 ) ){ return true; } var allowed = new RegExp('[' + this.getFeature('zpFormAllowedChars') + ']'); this.setValueFromField(); if (!(allowed.test(newChar))) { Zapatec.Utils.addClass(this.field, "zpWrongValue"); this.field.readonly = true; var self = this; setTimeout(function(){ Zapatec.Utils.removeClass(self.field, "zpWrongValue"); self.field.readonly = false; }, 100); return false; } return true; } if(this.hasFeature("zpFormMask")){ var self = this; var tmpArr = Zapatec.Utils.getCharFromEvent(evt) var charCode = tmpArr.charCode; var newChar = tmpArr.chr; if(this.processFunctionalKeys(evt) == true){ return true; } this.setValueFromField(); var pos = this.getSelectionStart(); if(charCode == null && newChar == null){ return false; } if(!Zapatec.is_ie){ if(Zapatec.Form.Utils.isSpecialKey(charCode, newChar)){ return true; } if(this.processCustomKeys(charCode) == false){ return false; } } // if char under cursor is strict - search for next mask char. // If no such char founded - leave at current position and exit if(typeof(this.chars[pos]) == 'string'){ var newPos = this.getNextAvailablePosition(pos); if(newPos == null || newPos == pos) return false; this.setCaretPosition(newPos); pos = newPos; } // check if entered char could be applied to current mask element. if( pos >= this.chars.length || typeof(this.chars[pos]) != 'string' && !newChar.match(this.chars[pos]) || typeof(this.chars[pos]) == 'string' && newChar != this.chars[pos] ){ Zapatec.Utils.addClass(this.field, "zpWrongValue"); this.field.readonly = true; setTimeout(function(){ Zapatec.Utils.removeClass(self.field, "zpWrongValue"); self.field.readonly = false; }, 100); this.setValue(); this.setCaretPosition(pos); } else { // all is ok. store and display entered char. this.enteredValue[pos] = newChar; this.setValue(); var newPos = this.getNextAvailablePosition(pos); if(newPos == null){ newPos = pos + 1; } this.setCaretPosition(newPos); } if(evt && evt.preventDefault){ evt.preventDefault(); } return false; } return true;};/** * @private * Handler for keyup event. * @param {Event} event object */Zapatec.Form.Field.prototype.keyup = function(evt) { if(!this.isEditing){ return false; } if(!evt){ evt = window.event; } this.fireEvent("keyUp", evt); this.fireEvent("all", evt, "keyup"); this.form.fireEvent("keyUp", evt); this.form.fireEvent("all", evt, "keyup"); if(evt){ var tmp = Zapatec.Utils.getCharFromEvent(evt); if( Zapatec.Form.Utils.isSpecialKey(tmp.charCode, tmp.chr) || ( ( tmp.charCode == 8 || tmp.charCode == 46 ) && this.state.lastSelectionStart != this.state.lastSelectionEnd ) ){ return true; } } this.validate(); if(this.hasFeature("zpFormAutoComplete") || this.hasFeature("zpFormAutoCompleteStrict")){ this.autoCompleteValue(this.getAutoCompleteOptions()); } this.keyPressCounter++; var self = this; setTimeout(function(){self.runDelayedActions()}, Zapatec.Form.Field.DELAYED_INTERVAL); return true;};/** * @private * handler for focus event. * @param {Event} event object */Zapatec.Form.Field.prototype.focus = function(evt) { if(!evt){ evt = window.event; } if(this.field.readOnly){ return; } this.isEditing = true; this.firstRun = false; if(this.hasFeature("zpFormMask")){ if(this.isEmpty()){ this.setValue(); this.setCaretPosition(0); } } if(this.isBooleanField){ this.booleanChanged(evt); } this.fireEvent("focus", evt); this.fireEvent("all", evt, "focus"); this.form.fireEvent("focus", evt); this.form.fireEvent("all", evt, "focus"); this.validate();};/** * @private * handler for blur event. * @param {Event} event object */Zapatec.Form.Field.prototype.blur = function(evt) { if(!evt){ evt = window.event; } // clean mask layout from field if no value was entered if(this.hasFeature("zpFormMask") && !this.isFilled()){ Zapatec.Form.Utils.setValue(this.field, ""); } if(!this.isEditing){ return; } this.isEditing = false; if(this.hasFeature("zpFormAllowedChars")){ this.setValueFromField(true); } this.fireEvent("blur", evt); this.fireEvent("all", evt, "blur"); this.form.fireEvent("blur", evt); this.form.fireEvent("all", evt, "blur"); this.validate();};/** * @private * function to validate current field. * @param onlyValidate {boolean} If true - only validate field. Else - also * display error message. * @return array of error in format * [ * { * 'field': [string], // field name * 'errorMessage': [string], // error message * 'validator': [string] // validator name * }, * ... * ] * @type Object */Zapatec.Form.Field.prototype.validate = function(onlyValidate) { if ( !this.field.className || ( this.field.disabled && !this.firstRun ) ){ return null; } var message = null; var errors = []; var isRequired = this.isRequired(); // performance optimizations var isEmpty = this.isEmpty(); if(this.isBooleanField && !isRequired){ isEmpty = this.field.checked; } if(this.firstRun && !isEmpty || this.field.disabled){ this.firstRun = false; } var validatorUsed = isRequired; var validatorName = null; var l10nMessage = null; var messageArgument = null; function addError(self, errors, validatorName, l10nMessage, messageArgument){ if(!validatorName){ return null } var message = self.hasFeature(validatorName + "Error") ? self.getFeature(validatorName + "Error") : self.getMessage(l10nMessage, messageArgument); if(!message){ message = self.getMessage(l10nMessage, messageArgument); } message = message.replace(/&/g, '&').replace(/</g, '<').replace(/\>/g, '>'); errors.push({ field: self.field, errorMessage: message, validator: validatorName }); return message; } // If field is empty - do not run any validations. Only display error if // field is required. if(isEmpty){ validatorUsed = true; if(isRequired){ validatorName = "zpFormRequired"; l10nMessage = "isRequiredError"; } } else { for(var vName in this.features){ if(vName == 'zpFormMask'){ validatorUsed = true; if(!this.isMaskFullyFilled()){ validatorName = "zpFormMask"; l10nMessage = 'maskNotFilledError'; messageArgument = this.getFeature("zpFormMask"); // do not run any other validators until mask is filled. break; } } if(vName == 'zpFormAutoCompleteStrict'){ validatorUsed = true; var found = false; var currVal = this.getValue(); for(var ii = this.autoCompleteOptions.length - 1; ii >= 0 ; ii--){ if(currVal == this.autoCompleteOptions[ii]){ found = true; break; } } if(!found){ validatorName = "zpFormAutoCompleteStrict"; l10nMessage = "noSuchAutoCompleteValueError"; } } if(typeof(Zapatec.Form.dataTypes[vName]) != 'undefined'){ validatorUsed = true; var validator = Zapatec.Form.dataTypes[vName]; var validatorPassed = true; messageArgument = this.getFeature(vName); if(validator.regex){ // Regex validation. validatorPassed = validator.regex.test(this.getValue()); } else if (validator.func){ // Javascript function validation. validatorPassed = validator.func(this.getValue(), this.getFeature(vName)); } if(!validatorPassed){ validatorName = vName; l10nMessage = validator.error; } } if(validatorName){ message = addError(this, errors, validatorName, l10nMessage, messageArgument) || message; validatorName = null; } } if(this.ajaxError != null){ validatorUsed = true; validatorName = "zpFormValidate"; l10nMessage = this.ajaxError; } } message = addError(this, errors, validatorName, l10nMessage, messageArgument) || message; if(!onlyValidate && validatorUsed){ this.setImageStatus(message, isEmpty); } return errors;};/** * @private * Sets the CLASS of the status indicator next to a form field, and its title * tooltip popup. * @param status - [string] error message * @param isEmpty - [boolean] Optional. Optimization tip. Do not use it. */Zapatec.Form.Field.prototype.setImageStatus = function(status, isEmpty) { var isRequired = this.isRequired(); if(typeof(isEmpty) == 'undefined'){ isEmpty = this.isEmpty(); } // clear all marks from status fields. this.requiredMark.className = Zapatec.Form.IGNORE_CLASSNAME + (isRequired ? ' zpIsRequired' : ' zpNotRequired'); this.editingMark.className = Zapatec.Form.IGNORE_CLASSNAME; this.emptyMark.className = Zapatec.Form.IGNORE_CLASSNAME; this.validMark.className = Zapatec.Form.IGNORE_CLASSNAME; this.errorText.innerHTML = ""; if(this.config.formConfig.strict){ if(status == null){ this.form.toggleSubmits(this.form.validate() != null); } else { this.form.toggleSubmits(true); } } // process field only if this is not first round mark if( !this.firstRun && ( isRequired && isEmpty || !isEmpty ) ){ this.editingMark.className = Zapatec.Form.IGNORE_CLASSNAME + (this.isEditing ? ' zpIsEditing' : ' zpNotEditing'); this.emptyMark.className = Zapatec.Form.IGNORE_CLASSNAME + (isEmpty ? ' zpIsEmpty' : ' zpNotEmpty'); this.validMark.className = Zapatec.Form.IGNORE_CLASSNAME + (!status ? ' zpIsValid' : ' zpNotValid'); // if field is empty but it is editing in this time - do not display // "this field is required" message. if( ( // do not display error if field is required and it is empty during editing. isRequired && isEmpty && this.isEditing ) || ( // do not display error while typing if displayErrorWhileTyping is false this.isEditing && !this.config.formConfig.displayErrorWhileTyping ) ){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -