Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. Vue.component('validation-form', {
  2. template: '<div class="validation-form"><slot></slot>{{results}}</div>',
  3. data () {
  4. return {
  5. results: {}
  6. }
  7. },
  8. methods: {
  9. setValidity(name, validity) {
  10. if (this.results[name] !== validity) {
  11. this.results[name] = validity
  12. console.log(this.results)
  13. this.$forceUpdate()
  14. }
  15. }
  16. }
  17. })
  18.  
  19. Vue.directive('validate', {
  20. update: function (el, binding, vnode) {
  21. const validationForm = findValidationFormFromAncestors(el, vnode)
  22. if (validationForm) {
  23. validationForm.setValidity(el.id, el.value && el.value.length > 0)
  24. }
  25. }
  26. })
  27.  
  28. function findValidationFormFromAncestors (el, vnode) {
  29. const validationFormElement = findValidationFormElementFromAncestors(el)
  30. if (validationFormElement) {
  31. return findValidationFormFromChildren(vnode.context, validationFormElement)
  32. } else {
  33. return null
  34. }
  35. }
  36.  
  37. function findValidationFormFromChildren(component, validationFormElement) {
  38. if (component.$el === validationFormElement) {
  39. return component
  40. } else {
  41. for (let i = 0; i < component.$children.length; ++i) {
  42. const found = findValidationFormFromChildren(component.$children[i], validationFormElement)
  43. if (found) {
  44. return found
  45. }
  46. }
  47. return null
  48. }
  49. }
  50.  
  51. function findValidationFormElementFromAncestors (el) {
  52. if (el.parentElement) {
  53. if (el.parentElement.className === 'validation-form') {
  54. return el.parentElement
  55. } else {
  56. return findValidationFormElementFromAncestors(el.parentElement)
  57. }
  58. } else {
  59. return null
  60. }
  61. }
  62.  
  63. new Vue({
  64. el: '#validation-demo',
  65. data: {
  66. id: '',
  67. passwd: ''
  68. },
  69. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement