1/* 2--- 3 4script: Form.Validator.Inline.js 5 6name: Form.Validator.Inline 7 8description: Extends Form.Validator to add inline messages. 9 10license: MIT-style license 11 12authors: 13 - Aaron Newton 14 15requires: 16 - Form.Validator 17 18provides: [Form.Validator.Inline] 19 20... 21*/ 22 23Form.Validator.Inline = new Class({ 24 25 Extends: Form.Validator, 26 27 options: { 28 showError: function(errorElement){ 29 if (errorElement.reveal) errorElement.reveal(); 30 else errorElement.setStyle('display', 'block'); 31 }, 32 hideError: function(errorElement){ 33 if (errorElement.dissolve) errorElement.dissolve(); 34 else errorElement.setStyle('display', 'none'); 35 }, 36 scrollToErrorsOnSubmit: true, 37 scrollToErrorsOnBlur: false, 38 scrollToErrorsOnChange: false, 39 scrollFxOptions: { 40 transition: 'quad:out', 41 offset: { 42 y: -20 43 } 44 } 45 }, 46 47 initialize: function(form, options){ 48 this.parent(form, options); 49 this.addEvent('onElementValidate', function(isValid, field, className, warn){ 50 var validator = this.getValidator(className); 51 if (!isValid && validator.getError(field)){ 52 if (warn) field.addClass('warning'); 53 var advice = this.makeAdvice(className, field, validator.getError(field), warn); 54 this.insertAdvice(advice, field); 55 this.showAdvice(className, field); 56 } else { 57 this.hideAdvice(className, field); 58 } 59 }); 60 }, 61 62 makeAdvice: function(className, field, error, warn){ 63 var errorMsg = (warn) ? this.warningPrefix : this.errorPrefix; 64 errorMsg += (this.options.useTitles) ? field.title || error:error; 65 var cssClass = (warn) ? 'warning-advice' : 'validation-advice'; 66 var advice = this.getAdvice(className, field); 67 if (advice){ 68 advice = advice.set('html', errorMsg); 69 } else { 70 advice = new Element('div', { 71 html: errorMsg, 72 styles: { display: 'none' }, 73 id: 'advice-' + className.split(':')[0] + '-' + this.getFieldId(field) 74 }).addClass(cssClass); 75 } 76 field.store('$moo:advice-' + className, advice); 77 return advice; 78 }, 79 80 getFieldId : function(field){ 81 return field.id ? field.id : field.id = 'input_' + field.name; 82 }, 83 84 showAdvice: function(className, field){ 85 var advice = this.getAdvice(className, field); 86 if ( 87 advice && 88 !field.retrieve('$moo:' + this.getPropName(className)) && 89 ( 90 advice.getStyle('display') == 'none' || 91 advice.getStyle('visibility') == 'hidden' || 92 advice.getStyle('opacity') == 0 93 ) 94 ){ 95 field.store('$moo:' + this.getPropName(className), true); 96 this.options.showError(advice); 97 this.fireEvent('showAdvice', [field, advice, className]); 98 } 99 }, 100 101 hideAdvice: function(className, field){ 102 var advice = this.getAdvice(className, field); 103 if (advice && field.retrieve('$moo:' + this.getPropName(className))){ 104 field.store('$moo:' + this.getPropName(className), false); 105 this.options.hideError(advice); 106 this.fireEvent('hideAdvice', [field, advice, className]); 107 } 108 }, 109 110 getPropName: function(className){ 111 return 'advice' + className; 112 }, 113 114 resetField: function(field){ 115 field = document.id(field); 116 if (!field) return this; 117 this.parent(field); 118 field.get('validators').each(function(className){ 119 this.hideAdvice(className, field); 120 }, this); 121 return this; 122 }, 123 124 getAllAdviceMessages: function(field, force){ 125 var advice = []; 126 if (field.hasClass('ignoreValidation') && !force) return advice; 127 var validators = field.get('validators').some(function(cn){ 128 var warner = cn.test('^warn-') || field.hasClass('warnOnly'); 129 if (warner) cn = cn.replace(/^warn-/, ''); 130 var validator = this.getValidator(cn); 131 if (!validator) return; 132 advice.push({ 133 message: validator.getError(field), 134 warnOnly: warner, 135 passed: validator.test(), 136 validator: validator 137 }); 138 }, this); 139 return advice; 140 }, 141 142 getAdvice: function(className, field){ 143 return field.retrieve('$moo:advice-' + className); 144 }, 145 146 insertAdvice: function(advice, field){ 147 //Check for error position prop 148 var props = field.get('validatorProps'); 149 //Build advice 150 if (!props.msgPos || !document.id(props.msgPos)){ 151 if (field.type && field.type.toLowerCase() == 'radio') field.getParent().adopt(advice); 152 else advice.inject(document.id(field), 'after'); 153 } else { 154 document.id(props.msgPos).grab(advice); 155 } 156 }, 157 158 validateField: function(field, force, scroll){ 159 var result = this.parent(field, force); 160 if (((this.options.scrollToErrorsOnSubmit && scroll == null) || scroll) && !result){ 161 var failed = document.id(this).getElement('.validation-failed'); 162 var par = document.id(this).getParent(); 163 while (par != document.body && par.getScrollSize().y == par.getSize().y){ 164 par = par.getParent(); 165 } 166 var fx = par.retrieve('$moo:fvScroller'); 167 if (!fx && window.Fx && Fx.Scroll){ 168 fx = new Fx.Scroll(par, this.options.scrollFxOptions); 169 par.store('$moo:fvScroller', fx); 170 } 171 if (failed){ 172 if (fx) fx.toElement(failed);
173 else par.scrollTo(par.getScroll().x, failed.getPosition(par).y - 20); 174 } 175 } 176 return result; 177 }, 178 179 watchFields: function(fields){ 180 fields.each(function(el){ 181 if (this.options.evaluateFieldsOnBlur){ 182 el.addEvent('blur', this.validationMonitor.pass([el, false, this.options.scrollToErrorsOnBlur], this)); 183 } 184 if (this.options.evaluateFieldsOnChange){ 185 el.addEvent('change', this.validationMonitor.pass([el, true, this.options.scrollToErrorsOnChange], this)); 186 } 187 }, this); 188 } 189 190});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.