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.

124 lines
3.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <field-error>
  2. <div class="field-error" if={ state.errors.length > 0 }>
  3. <ul>
  4. <li each={ error in state.errors }>
  5. { error }
  6. </li>
  7. </ul>
  8. </div>
  9. <script>
  10. /**
  11. * Shows errors of Validation and raise a element to
  12. * trigger single validation of field
  13. *
  14. * <field-error name="name" closest="<optional>" nofieldupdate></field-error>
  15. *
  16. * @author Björn Hase, Tentakelfabrik, me@tentakelfabrik.de
  17. *
  18. */
  19. export default {
  20. state: {
  21. errors: [
  22. ],
  23. // css class for
  24. closest: '.field-group',
  25. },
  26. /**
  27. *
  28. *
  29. * @param {Object} props
  30. * @param {Object} state
  31. *
  32. */
  33. onBeforeMounted(props, state)
  34. {
  35. if (props.closest) {
  36. state.closest = props.closest
  37. }
  38. },
  39. /**
  40. *
  41. *
  42. * @param {Object} props
  43. * @param {Object} state
  44. *
  45. */
  46. onMounted(props, state)
  47. {
  48. // getting parent element for entire field
  49. const parent = this.root.closest(state.closest)
  50. // getting current element by name
  51. const element = parent.querySelector('[name="' + props.name + '"]')
  52. // getting form
  53. const form = element.closest('form')
  54. // element, form are exists and nofieldupdate is not set
  55. // each change of the element dispatch a event to form validation
  56. if (element && form && !props.nofieldupdate) {
  57. element.addEventListener('input', (event) => {
  58. this.dispatchCustomEvent(event, form, props.name)
  59. })
  60. }
  61. // add custom event to listen to form-validation
  62. this.root.addEventListener('form-validation', (event) => {
  63. this.onFormValidation(event, parent)
  64. })
  65. },
  66. /**
  67. * process form validation triggered by form
  68. *
  69. * @param {Event} event
  70. * @param {Element} parent
  71. *
  72. */
  73. onFormValidation(event, parent)
  74. {
  75. // if detail is a value, set to errors
  76. if (event.detail) {
  77. this.state.errors = event.detail
  78. parent.classList.add('field--error')
  79. parent.classList.remove('field--valid')
  80. } else {
  81. this.state.errors = []
  82. parent.classList.remove('field--error')
  83. parent.classList.add('field--valid')
  84. }
  85. this.update()
  86. },
  87. /**
  88. * create event to send to form validation
  89. *
  90. * @param {Event} event
  91. * @param {Element} form
  92. * @param {string} name
  93. *
  94. */
  95. dispatchCustomEvent(event, form, name)
  96. {
  97. const fieldUpdateEvent = new CustomEvent('field-update', {
  98. 'detail': {
  99. 'name': name,
  100. 'value': event.target.value
  101. }
  102. })
  103. form.dispatchEvent(fieldUpdateEvent)
  104. },
  105. }
  106. </script>
  107. </field-error>