|
1 | 1 | import { el, uid } from './helpers'
|
2 |
| -const { __ } = wp.i18n |
3 |
| -import 'parsleyjs' |
| 2 | +const { __, sprintf } = wp.i18n |
| 3 | +let fieldOptions = { |
| 4 | + text: { |
| 5 | + multiple: false, |
| 6 | + }, |
| 7 | + textarea: { |
| 8 | + multiple: false, |
| 9 | + }, |
| 10 | + email: { |
| 11 | + multiple: false, |
| 12 | + rules: { |
| 13 | + email: __("This value should be a valid email", "formality"), |
| 14 | + } |
| 15 | + }, |
| 16 | + number: { |
| 17 | + multiple: false, |
| 18 | + rules: { |
| 19 | + number: __("This value should be a valid number", "formality"), |
| 20 | + number_min: /* translators: validation */ __("This value should be greater than or equal to %s", "formality"), |
| 21 | + number_max: /* translators: validation */ __("This value should be lower than or equal to %s", "formality"), |
| 22 | + } |
| 23 | + }, |
| 24 | + select: { |
| 25 | + multiple: false, |
| 26 | + }, |
| 27 | + multiple: { |
| 28 | + multiple: true, |
| 29 | + }, |
| 30 | + rating: { |
| 31 | + multiple: true, |
| 32 | + }, |
| 33 | + switch: { |
| 34 | + multiple: false, |
| 35 | + }, |
| 36 | + upload: { |
| 37 | + multiple: false, |
| 38 | + rules: { |
| 39 | + file: __("This is not a valid file", "formality"), |
| 40 | + file_ext: /* translators: validation */ __('%s file extension is not allowed', 'formality'), |
| 41 | + file_size: __('Your file exceeds the size limit', 'formality'), |
| 42 | + } |
| 43 | + }, |
| 44 | +} |
4 | 45 |
|
5 | 46 | export default {
|
6 | 47 | init() {
|
7 | 48 | //init validation
|
8 |
| - $(el("form")).each(function() { |
9 |
| - uid($(this)) |
10 |
| - $(el("section", "uid")).each(function(index, section) { |
11 |
| - $(section).find(':input').attr('data-parsley-group', 'step-' + index) |
| 49 | + let validate = this; |
| 50 | + const forms = document.querySelectorAll(el("form")) |
| 51 | + forms.forEach(function(form){ |
| 52 | + validate.addStepIndexes(form) |
| 53 | + const inputs = form.querySelectorAll('input, select, textarea') |
| 54 | + inputs.forEach(function(input){ |
| 55 | + validate.liveUpdate(input) |
12 | 56 | })
|
13 | 57 | })
|
14 |
| - this.field_error() |
15 |
| - this.field_success() |
16 |
| - this.form_error() |
17 |
| - this.i18n() |
18 |
| - }, |
19 |
| - checkstep(index, newindex) { |
20 |
| - //validate single step |
21 |
| - let valid = false |
22 |
| - let options = this.parsley_options() |
23 |
| - if(index > newindex) { |
24 |
| - valid = true |
25 |
| - } else { |
26 |
| - $(el("form", "uid")).parsley(options).whenValidate({ |
27 |
| - group: 'step-' + index, |
28 |
| - }).done(function() { |
29 |
| - valid = true |
30 |
| - $(el("nav_section", "uid")).eq(index).addClass(el("nav_section", false, "--validated")) |
| 58 | + }, |
| 59 | + addStepIndexes(form) { |
| 60 | + const sections = form.querySelectorAll(el("section")) |
| 61 | + sections.forEach(function(section, index){ |
| 62 | + const fields = section.querySelectorAll(el("field")) |
| 63 | + fields.forEach(function(field){ |
| 64 | + field.setAttribute('data-step', index) |
31 | 65 | })
|
| 66 | + }) |
| 67 | + }, |
| 68 | + liveUpdate(input) { |
| 69 | + let validate = this; |
| 70 | + input.addEventListener('input', function(){ |
| 71 | + let field = input.closest(el("field")) |
| 72 | + validate.validateField(field, true) |
| 73 | + }) |
| 74 | + }, |
| 75 | + validateStep(form, index, newindex) { |
| 76 | + if(index > newindex) { return true } |
| 77 | + const valid = this.validateForm(form, index) |
| 78 | + if(valid) { |
| 79 | + const sections = form.querySelectorAll(el("nav_section", "uid")) |
| 80 | + sections[index].classList.add(el("nav_section", false, "--validated")) |
32 | 81 | }
|
33 | 82 | return valid
|
34 | 83 | },
|
35 |
| - form() { |
36 |
| - //validate standard form (1 step) |
37 |
| - let options = this.parsley_options() |
38 |
| - $(el("form", "uid")).parsley(options) |
39 |
| - }, |
40 |
| - parsley_options() { |
41 |
| - //create parsley options array |
42 |
| - let options = { |
43 |
| - classHandler: function (element) { |
44 |
| - return element.$element.closest(el("field")) |
45 |
| - }, |
46 |
| - errorClass: el("field_error", false), |
47 |
| - errorsContainer: function(element) { |
48 |
| - return element.$element.closest(el("input")).find(el("input", true, "__status")) |
49 |
| - }, |
50 |
| - successClass: el("field_success", false), |
51 |
| - errorsWrapper: '<ul class="'+el("input_errors", false)+'"></ul>', |
| 84 | + checkRule(input, rule) { |
| 85 | + let result = { |
| 86 | + valid: false, |
| 87 | + placeholder: '', |
52 | 88 | }
|
53 |
| - return options |
| 89 | + switch(rule) { |
| 90 | + case 'required': |
| 91 | + if(NodeList.prototype.isPrototypeOf(input)){ |
| 92 | + input.forEach(function(single, i){ if(single.checked) { result.valid = true; } }) |
| 93 | + } else { |
| 94 | + result.valid = input.value !== '' |
| 95 | + } |
| 96 | + break |
| 97 | + case 'email': |
| 98 | + result.valid = input.value.match(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/) |
| 99 | + break |
| 100 | + case 'checked': |
| 101 | + result.valid = input.checked |
| 102 | + break |
| 103 | + case 'notchecked': |
| 104 | + result.valid = !input.checked |
| 105 | + break |
| 106 | + case 'number': |
| 107 | + result.valid = !isNaN(input.value) |
| 108 | + break |
| 109 | + case 'number_min': |
| 110 | + result.placeholder = input.min |
| 111 | + result.valid = parseFloat(input.value) >= result.placeholder |
| 112 | + break |
| 113 | + case 'number_max': |
| 114 | + result.placeholder = input.max |
| 115 | + result.valid = parseFloat(input.value) <= result.placeholder |
| 116 | + break |
| 117 | + case 'file': |
| 118 | + result.valid = input.files.length ? true : false |
| 119 | + break |
| 120 | + case 'file_ext': |
| 121 | + result.file = input.files.length ? input.files[0] : false |
| 122 | + if(result.file && result.file.type !== '') { |
| 123 | + result.formats = input.getAttribute('accept').split(", ") |
| 124 | + result.placeholder = ('.' + result.file.name.split('.').pop()).toLowerCase() |
| 125 | + result.valid = result.formats.indexOf(result.placeholder) !== -1 |
| 126 | + } |
| 127 | + break |
| 128 | + case 'file_size': |
| 129 | + result.file = input.files.length ? input.files[0] : false |
| 130 | + if(result.file && result.file.size > 0) { |
| 131 | + result.valid = result.file.size <= parseInt(input.getAttribute('data-max-size')) |
| 132 | + } |
| 133 | + break |
| 134 | + } |
| 135 | + return result; |
54 | 136 | },
|
55 |
| - form_error() { |
56 |
| - window.Parsley.on('form:error', function() { |
57 |
| - |
58 |
| - }) |
| 137 | + changeFieldStatus(field, name, valid=true, error='') { |
| 138 | + const form = field.closest(el('form')) |
| 139 | + const status = field.querySelector(el('input_status')) |
| 140 | + const legend = form.querySelector(el('nav_legend', true, ' li[data-name="' + name + '"]')) |
| 141 | + field.classList.toggle(el("field", false, "--error"), !valid); |
| 142 | + status.innerHTML = !error ? '' : ('<div class="' + el("input_errors", false) + '">' + error + '</div>') |
| 143 | + if(legend) { legend.classList.toggle("error", !valid) } |
| 144 | + if(!valid) { |
| 145 | + const section = form.querySelector(el("nav_section", "uid", "--active")) |
| 146 | + if(section) { section.classList.remove(el("nav_section", false, "--validated")) } |
| 147 | + } |
| 148 | + field.classList.add(el("field", false, "--validated")); |
59 | 149 | },
|
60 |
| - field_error() { |
61 |
| - //field error event |
62 |
| - window.Parsley.on('field:error', function() { |
63 |
| - const id = $(this.$element).attr("id") |
64 |
| - uid($(this.$element)) |
65 |
| - $(el("nav_legend", 'uid', ' li[data-name="' + id + '"]')).addClass("error") |
66 |
| - const index = $(el("nav_section", "uid")).index(el("nav_section", "uid", "--active")) |
67 |
| - $(el("nav_section", "uid")).eq(index).removeClass(el("nav_section", false, "--validated")) |
68 |
| - }) |
| 150 | + validateField(field, soft=false) { |
| 151 | + if(field.hasAttribute('data-excluded')) { return true } |
| 152 | + let validate = this; |
| 153 | + const type = field.getAttribute('data-type') |
| 154 | + const required = field.classList.contains(el("field", false, "--required")) |
| 155 | + const validated = field.classList.contains(el("field", false, "--validated")) |
| 156 | + let rules = 'rules' in fieldOptions[type] ? Object.keys(fieldOptions[type]['rules']) : [] |
| 157 | + const multiple = fieldOptions[type]['multiple'] |
| 158 | + if(required) { rules.unshift('required') } |
| 159 | + const input = multiple ? field.querySelectorAll('input, select, textarea') : field.querySelector('input, select, textarea') |
| 160 | + const name = multiple ? input[0].name : input.name |
| 161 | + let valid = true; |
| 162 | + let error = ''; |
| 163 | + if(!rules.includes('required') && !multiple && !input.value) { |
| 164 | + //skip validation |
| 165 | + } else { |
| 166 | + rules.forEach(function(rule){ |
| 167 | + if(valid) { |
| 168 | + const check = validate.checkRule(input, rule) |
| 169 | + if(!check.valid) { |
| 170 | + error = rule == 'required' ? __("This value is required", "formality") : sprintf(fieldOptions[type]['rules'][rule], check.placeholder); |
| 171 | + valid = false; |
| 172 | + } |
| 173 | + } |
| 174 | + }) |
| 175 | + } |
| 176 | + if(!soft || (soft && validated)) { |
| 177 | + validate.changeFieldStatus(field, name, valid, error) |
| 178 | + } |
| 179 | + return valid; |
69 | 180 | },
|
70 |
| - field_success() { |
71 |
| - //field success event |
72 |
| - window.Parsley.on('field:success', function() { |
73 |
| - const id = $(this.$element).attr("id") |
74 |
| - uid($(this.$element)) |
75 |
| - $(el("nav_legend", "uid", ' li[data-name="' + id + '"]')).removeClass("error") |
| 181 | + validateForm(form, step=null) { |
| 182 | + let validate = this |
| 183 | + let errors = false |
| 184 | + const selector = step == null ? el("field") : el("field", true, '[data-step="'+step+'"]') |
| 185 | + let fields = form.querySelectorAll(selector) |
| 186 | + let firsterror = false |
| 187 | + fields.forEach(function(field, i){ |
| 188 | + const error = !validate.validateField(field) |
| 189 | + if(!errors && error) { |
| 190 | + errors = true |
| 191 | + firsterror = field.querySelector('input, select, textarea') |
| 192 | + } |
76 | 193 | })
|
77 |
| - }, |
78 |
| - i18n() { |
79 |
| - window.Parsley.addMessages('en', { |
80 |
| - defaultMessage: __("This value seems to be invalid", "formality"), |
81 |
| - type: { |
82 |
| - email: __("This value should be a valid email", "formality"), |
83 |
| - url: __("This value should be a valid url", "formality"), |
84 |
| - number: __("This value should be a valid number", "formality"), |
85 |
| - integer: __("This value should be a valid integer", "formality"), |
86 |
| - digits: __("This value should be digits", "formality"), |
87 |
| - alphanum: __("This value should be alphanumeric", "formality"), |
88 |
| - }, |
89 |
| - required: __("This value is required", "formality"), |
90 |
| - pattern: __("This value seems to be invalid", "formality"), |
91 |
| - min: /* translators: validation */ __("This value should be greater than or equal to %s", "formality"), |
92 |
| - max: /* translators: validation */ __("This value should be lower than or equal to %s", "formality"), |
93 |
| - range: /* translators: validation */ __("This value should be between %s and %s", "formality"), |
94 |
| - minlength: /* translators: validation */ __("This value is too short. It should have %s characters or more", "formality"), |
95 |
| - maxlength: /* translators: validation */ __("This value is too long. It should have %s characters or fewer", "formality"), |
96 |
| - length: /* translators: validation */ __("This value length is invalid. It should be between %s and %s characters long", "formality"), |
97 |
| - check: /* translators: validation */ __("You must select between %s and %s choices", "formality"), |
98 |
| - }); |
99 |
| - window.Parsley.setLocale('en'); |
| 194 | + if(firsterror) { firsterror.focus() } |
| 195 | + return !errors |
100 | 196 | }
|
101 | 197 | }
|
0 commit comments