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