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.

146 lines
3.3 KiB

3 years ago
3 years ago
3 years ago
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. if (!this.form) {
  26. console.error('FormValidator: form not found, querySelector not found "' + this.formSelector + '"')
  27. }
  28. this.elements = this.form.querySelectorAll('field-error')
  29. // adding submit event
  30. this.form.addEventListener('submit', (event) => {
  31. this.onSubmit(event)
  32. })
  33. // adding event if a element is updated
  34. this.form.addEventListener('field-update', (event) => {
  35. this.onFieldUpdate(event)
  36. })
  37. this.onSuccess = onSuccess
  38. }
  39. /**
  40. *
  41. *
  42. */
  43. setConstraits(constraits)
  44. {
  45. this.constraits = constraits
  46. }
  47. /**
  48. *
  49. *
  50. */
  51. getConstraits(constraits)
  52. {
  53. return this.constraits
  54. }
  55. /**
  56. *
  57. * @param {[type]} event [description]
  58. * @return {[type]} [description]
  59. */
  60. onSubmit(event)
  61. {
  62. event.preventDefault()
  63. let errors = validate(serialize(event.target, {
  64. hash: true
  65. }), this.constraits, {
  66. fullMessages: false
  67. })
  68. if (errors) {
  69. // send each element a event
  70. this.elements.forEach((element) => {
  71. let elementErrors = false
  72. // check for errors by name
  73. if (errors[element.attributes.name.nodeValue]) {
  74. elementErrors = errors[element.attributes.name.nodeValue]
  75. }
  76. this.dispatchCustomEvent(elementErrors, element)
  77. })
  78. } else {
  79. this.onSuccess(event, serialize(event.target, {
  80. hash: true
  81. }))
  82. }
  83. }
  84. /**
  85. *
  86. *
  87. * @param {Event} event
  88. *
  89. */
  90. onFieldUpdate(event)
  91. {
  92. // workaround, make sure that value for single is undefined if it is empty
  93. if (event.detail.value == '') {
  94. event.detail.value = undefined
  95. }
  96. let errors = validate.single(event.detail.value, this.constraits[event.detail.name])
  97. // search for element by name and dispatch event
  98. this.elements.forEach((element) => {
  99. if (element.attributes.name.nodeValue == event.detail.name) {
  100. this.dispatchCustomEvent(errors, element)
  101. }
  102. })
  103. }
  104. /**
  105. * dispatch event to single element
  106. *
  107. * @param {Array} errors
  108. * @param {Element} element
  109. *
  110. */
  111. dispatchCustomEvent(errors, element)
  112. {
  113. let detail = false
  114. if (errors) {
  115. detail = errors
  116. }
  117. const formValidationEvent = new CustomEvent('form-validation', {
  118. 'detail': detail
  119. })
  120. element.dispatchEvent(formValidationEvent)
  121. }
  122. }
  123. export default FormValidator