You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.0 KiB

3 years ago
  1. import validate from 'validate.js'
  2. import serialize from 'form-serialize'
  3. /**
  4. * Form Validator with RiotJS Components
  5. *
  6. *
  7. *
  8. *
  9. */
  10. class FormValidator
  11. {
  12. /**
  13. *
  14. * @param {[type]} formSelector [description]
  15. * @param {[type]} constraits [description]
  16. */
  17. constructor(formSelector, constraits, onSuccess)
  18. {
  19. // getting selector to find form-element
  20. this.formSelector = formSelector
  21. // constraits for validate.js
  22. this.constraits = constraits
  23. // get form and elements
  24. this.form = document.querySelector(this.formSelector)
  25. this.elements = this.form.querySelectorAll('field-error')
  26. // adding submit event
  27. this.form.addEventListener('submit', (event) => {
  28. this.onSubmit(event)
  29. })
  30. // adding event if a element is updated
  31. this.form.addEventListener('field-update', (event) => {
  32. this.onFieldUpdate(event)
  33. })
  34. this.onSuccess = onSuccess
  35. }
  36. /**
  37. *
  38. * @param {[type]} event [description]
  39. * @return {[type]} [description]
  40. */
  41. onSubmit(event)
  42. {
  43. event.preventDefault()
  44. let errors = validate(serialize(event.target, {
  45. hash: true
  46. }), this.constraits, {
  47. fullMessages: false
  48. })
  49. if (errors) {
  50. // send each element a event
  51. this.elements.forEach((element) => {
  52. let elementErrors = false
  53. // check for errors by name
  54. if (errors[element.attributes.name.nodeValue]) {
  55. elementErrors = errors[element.attributes.name.nodeValue]
  56. }
  57. this.dispatchCustomEvent(elementErrors, element)
  58. })
  59. } else {
  60. this.onSuccess(event, serialize(event.target, {
  61. hash: true
  62. }))
  63. }
  64. }
  65. /**
  66. *
  67. *
  68. * @param {Event} event
  69. *
  70. */
  71. onFieldUpdate(event)
  72. {
  73. // workaround, make sure that value for single is undefined if it is empty
  74. if (event.detail.value == '') {
  75. event.detail.value = undefined
  76. }
  77. let errors = validate.single(event.detail.value, this.constraits[event.detail.name])
  78. // search for element by name and dispatch event
  79. this.elements.forEach((element) => {
  80. if (element.attributes.name.nodeValue == event.detail.name) {
  81. this.dispatchCustomEvent(errors, element)
  82. }
  83. })
  84. }
  85. /**
  86. * dispatch event to single element
  87. *
  88. * @param {Array} errors
  89. * @param {Element} element
  90. *
  91. */
  92. dispatchCustomEvent(errors, element)
  93. {
  94. let detail = false
  95. if (errors) {
  96. detail = errors
  97. }
  98. const formValidationEvent = new CustomEvent('form-validation', {
  99. 'detail': detail
  100. })
  101. element.dispatchEvent(formValidationEvent)
  102. }
  103. }
  104. export default FormValidator