Requires

Provides

Form.Validator.Inline.js

Extends Form.Validator to add inline messages.

License:
MIT-style license
Authors:
Aaron Newton
  1. 24
  2. 25
  3. 26
  4. 27
  5. 28
  6. 29
  7. 30
  8. 31
  9. 32
  10. 33
  11. 34
  12. 35
  13. 36
  14. 37
  15. 38
  16. 39
  17. 40
  18. 41
  19. 42
  20. 43
  21. 44
  22. 45
  23. 46
  24. 47
  25. 48
  26. 49
  27. 50
  28. 51
  29. 52
  30. 53
  31. 54
  32. 55
  33. 56
  34. 57
  35. 58
  36. 59
  37. 60
  38. 61
  39. 62
  40. 63
  41. 64
  42. 65
  43. 66
  44. 67
  45. 68
  46. 69
  47. 70
  48. 71
  49. 72
  50. 73
  51. 74
  52. 75
  53. 76
  54. 77
  55. 78
  56. 79
  57. 80
  58. 81
  59. 82
  60. 83
  61. 84
  62. 85
  63. 86
  64. 87
  65. 88
  66. 89
  67. 90
  68. 91
  69. 92
  70. 93
  71. 94
  72. 95
  73. 96
  74. 97
  75. 98
  76. 99
  77. 100
  78. 101
  79. 102
  80. 103
  81. 104
  82. 105
  83. 106
  84. 107
  85. 108
  86. 109
  87. 110
  88. 111
  89. 112
  90. 113
  91. 114
  92. 115
  93. 116
  94. 117
  95. 118
  96. 119
  97. 120
  98. 121
  99. 122
  100. 123
  101. 124
  102. 125
  103. 126
  104. 127
  105. 128
  106. 129
  107. 130
  108. 131
  109. 132
  110. 133
  111. 134
  112. 135
  113. 136
  114. 137
  115. 138
  116. 139
  117. 140
Form.Validator.Inline = new Class({ Extends: Form.Validator, options: { showError: function(errorElement){ if (errorElement.reveal) errorElement.reveal(); else errorElement.setStyle('display', 'block'); }, hideError: function(errorElement){ if (errorElement.dissolve) errorElement.dissolve(); else errorElement.setStyle('display', 'none'); }, scrollToErrorsOnSubmit: true, scrollFxOptions: { transition: 'quad:out', offset: { y: -20 } } }, initialize: function(form, options){ this.parent(form, options); this.addEvent('onElementValidate', function(isValid, field, className, warn){ var validator = this.getValidator(className); if (!isValid && validator.getError(field)){ if (warn) field.addClass('warning'); var advice = this.makeAdvice(className, field, validator.getError(field), warn); this.insertAdvice(advice, field); this.showAdvice(className, field); } else { this.hideAdvice(className, field); } }); }, makeAdvice: function(className, field, error, warn){ var errorMsg = (warn) ? this.warningPrefix : this.errorPrefix; errorMsg += (this.options.useTitles) ? field.title || error:error; var cssClass = (warn) ? 'warning-advice' : 'validation-advice'; var advice = this.getAdvice(className, field); if (advice){ advice = advice.set('html', errorMsg); } else { advice = new Element('div', { html: errorMsg, styles: { display: 'none' }, id: 'advice-' + className + '-' + this.getFieldId(field) }).addClass(cssClass); } field.store('$moo:advice-' + className, advice); return advice; }, getFieldId : function(field){ return field.id ? field.id : field.id = 'input_' + field.name; }, showAdvice: function(className, field){ var advice = this.getAdvice(className, field); if (advice && !field.retrieve('$moo:' + this.getPropName(className)) && (advice.getStyle('display') == 'none' || advice.getStyle('visiblity') == 'hidden' || advice.getStyle('opacity') == 0)){ field.store('$moo:' + this.getPropName(className), true); this.options.showError(advice); this.fireEvent('showAdvice', [field, advice, className]); } }, hideAdvice: function(className, field){ var advice = this.getAdvice(className, field); if (advice && field.retrieve('$moo:' + this.getPropName(className))){ field.store('$moo:' + this.getPropName(className), false); this.options.hideError(advice); this.fireEvent('hideAdvice', [field, advice, className]); } }, getPropName: function(className){ return 'advice' + className; }, resetField: function(field){ field = document.id(field); if (!field) return this; this.parent(field); field.className.split(' ').each(function(className){ this.hideAdvice(className, field); }, this); return this; }, getAllAdviceMessages: function(field, force){ var advice = []; if (field.hasClass('ignoreValidation') && !force) return advice; var validators = field.className.split(' ').some(function(cn){ var warner = cn.test('^warn-') || field.hasClass('warnOnly'); if (warner) cn = cn.replace(/^warn-/, ''); var validator = this.getValidator(cn); if (!validator) return; advice.push({ message: validator.getError(field), warnOnly: warner, passed: validator.test(), validator: validator }); }, this); return advice; }, getAdvice: function(className, field){ return field.retrieve('$moo:advice-' + className); }, insertAdvice: function(advice, field){

Check for error position prop

  1. 142
var props = field.get('validatorProps');

Build advice

  1. 144
  2. 145
  3. 146
  4. 147
  5. 148
  6. 149
  7. 150
  8. 151
  9. 152
  10. 153
  11. 154
  12. 155
  13. 156
  14. 157
  15. 158
  16. 159
  17. 160
  18. 161
  19. 162
  20. 163
  21. 164
  22. 165
  23. 166
  24. 167
  25. 168
  26. 169
  27. 170
  28. 171
  29. 172
  30. 173
if (!props.msgPos || !document.id(props.msgPos)){ if (field.type.toLowerCase() == 'radio') field.getParent().adopt(advice); else advice.inject(document.id(field), 'after'); } else { document.id(props.msgPos).grab(advice); } }, validateField: function(field, force){ var result = this.parent(field, force); if (this.options.scrollToErrorsOnSubmit && !result){ var failed = document.id(this).getElement('.validation-failed'); var par = document.id(this).getParent(); while (par != document.body && par.getScrollSize().y == par.getSize().y){ par = par.getParent(); } var fx = par.retrieve('$moo:fvScroller'); if (!fx && window.Fx && Fx.Scroll){ fx = new Fx.Scroll(par, this.options.scrollFxOptions); par.store('$moo:fvScroller', fx); } if (failed){ if (fx) fx.toElement(failed); else par.scrollTo(par.getScroll().x, failed.getPosition(par).y - 20); } } return result; } });