Requires

Provides

Validity.js

Validates widgets against preset rules

License:
Public domain (http://unlicense.org).
Authors:
Yaroslaff Fedin

There is a slight difference between this and a w3c spec. Spec states that as a result of a validation, there should be a .validity object on widget that holds all possible validation errors as keys and true or false as values.

Our .validity object doesnt not contain validations that passed successfuly and only holds errors. This gets it closer to ActiveRecord’s validation system.

  1. 36
  2. 37
  3. 38
  4. 39
  5. 40
  6. 41
  7. 42
  8. 43
  9. 44
  10. 45
  11. 46
  12. 47
  13. 48
  14. 49
  15. 50
  16. 51
  17. 52
  18. 53
  19. 54
  20. 55
  21. 56
  22. 57
  23. 58
  24. 59
  25. 60
  26. 61
  27. 62
  28. 63
  29. 64
  30. 65
  31. 66
  32. 67
  33. 68
  34. 69
  35. 70
  36. 71
  37. 72
  38. 73
  39. 74
  40. 75
  41. 76
  42. 77
  43. 78
  44. 79
  45. 80
  46. 81
  47. 82
  48. 83
  49. 84
  50. 85
  51. 86
  52. 87
  53. 88
  54. 89
  55. 90
  56. 91
  57. 92
  58. 93
  59. 94
  60. 95
  61. 96
  62. 97
  63. 98
  64. 99
  65. 100
  66. 101
  67. 102
  68. 103
  69. 104
  70. 105
  71. 106
  72. 107
!function() { LSD.Mixin.Validity = new Class({ behaviour: ':read-write', initialize: function() { this.parent.apply(this, arguments); this.addClass(this.attributes.required ? 'required' : 'optional'); }, checkValidity: function() { var validity = this.validity = {}; var value = this.getValue(); for (attribute in Attributes) { var constraint = this.attributes[attribute] if (!constraint) continue; var result = Attributes[attribute].call(this, value, constraint) if (!result) continue; validity[result] = true; } for (var i in validity) return !this.invalidate(); return this.validate(true); }, validate: function(value) { if (value !== true && !this.checkValidity()) return false; this.fireEvent('valid', value) this.setState('valid'); this.unsetState('invalid'); return true; }, invalidate: function(value) { this.setState('invalid'); this.unsetState('valid'); this.fireEvent('invalid', value); return true; }, setCustomValidity: function(validity) { this.validationMessage = validity; this.validity.customError = true; } }); var Attributes = LSD.Mixin.Validity.Attributes = { required: function(value) { if (!value) return "valueMissing" }, type: function(value, type) { if (!value.match()) return "typeMismatch" }, pattern: function(value, pattern) { if (!value.match(pattern)) return "patternMismatch" }, maxlength: function(value, length) { if ((value !== null) && (value.toString().length > length)) return "tooLong" }, min: function(value, min) { if (value < min) return "rangeUnderflow" }, max: function(value, max) { if (value > max) return "rangeOverflow" }, step: function(value, step) { if (value % step > 0) return "stepMismatch" } } }();