| 1 |
/** |
| 2 |
* Block Form |
| 3 |
*/ |
| 4 |
const { |
| 5 |
grecaptcha, |
| 6 |
GHOSTKIT: { googleReCaptchaAPISiteKey, events }, |
| 7 |
} = window; |
| 8 |
|
| 9 |
import { __ } from '@wordpress/i18n'; |
| 10 |
|
| 11 |
const errorParentSelector = |
| 12 |
'.ghostkit-form-field-name-first, .ghostkit-form-field-name-last, .ghostkit-form-field-email-primary, .ghostkit-form-field-email-confirm, .ghostkit-form-field'; |
| 13 |
|
| 14 |
function showError($field, message) { |
| 15 |
const $parent = $field.closest(errorParentSelector); |
| 16 |
|
| 17 |
if (!$parent) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
let $errorBox = $parent.querySelector('.ghostkit-form-field-error'); |
| 22 |
|
| 23 |
if (!$errorBox) { |
| 24 |
$errorBox = document.createElement('div'); |
| 25 |
$errorBox.classList.add('ghostkit-form-field-error'); |
| 26 |
$parent.append($errorBox); |
| 27 |
} |
| 28 |
|
| 29 |
$errorBox.innerHTML = message; |
| 30 |
$errorBox.setAttribute('aria-hidden', false); |
| 31 |
$field.setAttribute('aria-invalid', true); |
| 32 |
} |
| 33 |
|
| 34 |
function hideError($field) { |
| 35 |
const $parent = $field.closest(errorParentSelector); |
| 36 |
const $errorBox = $parent.querySelector('.ghostkit-form-field-error'); |
| 37 |
|
| 38 |
if ($errorBox) { |
| 39 |
$errorBox.innerHTML = ''; |
| 40 |
$errorBox.setAttribute('aria-hidden', true); |
| 41 |
} |
| 42 |
|
| 43 |
$field.setAttribute('aria-invalid', false); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Form validation. |
| 48 |
*/ |
| 49 |
events.on(document, 'init.blocks.gkt', () => { |
| 50 |
document |
| 51 |
.querySelectorAll('form.ghostkit-form:not(.ghostkit-form-ready)') |
| 52 |
.forEach(($form) => { |
| 53 |
$form.classList.add('ghostkit-form-ready'); |
| 54 |
|
| 55 |
// Disable native validation errors. |
| 56 |
$form.setAttribute('novalidate', ''); |
| 57 |
|
| 58 |
// Add 'invalid' event listener to all form fields separately |
| 59 |
// since this event does not bubbles. |
| 60 |
const fields = Array.from($form.elements); |
| 61 |
|
| 62 |
fields.forEach(($field) => { |
| 63 |
let customPatternError = ''; |
| 64 |
|
| 65 |
// Custom Email Confirm validation. |
| 66 |
if ($field.type === 'email' && $field.dataset.confirmEmail) { |
| 67 |
const refEmail = $form.querySelector( |
| 68 |
$field.dataset.confirmEmail |
| 69 |
); |
| 70 |
|
| 71 |
if (refEmail) { |
| 72 |
customPatternError = __( |
| 73 |
'These emails should match.', |
| 74 |
'ghostkit' |
| 75 |
); |
| 76 |
|
| 77 |
if (refEmail.getAttribute('required')) { |
| 78 |
$field.setAttribute( |
| 79 |
'required', |
| 80 |
refEmail.getAttribute('required') |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
events.on(refEmail, 'input', () => { |
| 85 |
if (refEmail.value) { |
| 86 |
$field.setAttribute('pattern', refEmail.value); |
| 87 |
$field.setAttribute('required', true); |
| 88 |
} else { |
| 89 |
$field.removeAttribute( |
| 90 |
'pattern', |
| 91 |
refEmail.value |
| 92 |
); |
| 93 |
|
| 94 |
if (refEmail.getAttribute('required')) { |
| 95 |
$field.setAttribute( |
| 96 |
'required', |
| 97 |
refEmail.getAttribute('required') |
| 98 |
); |
| 99 |
} else { |
| 100 |
$field.removeAttribute('required'); |
| 101 |
} |
| 102 |
} |
| 103 |
}); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
events.on($field, 'invalid', () => { |
| 108 |
if (customPatternError && $field.validity.patternMismatch) { |
| 109 |
showError($field, customPatternError); |
| 110 |
} else { |
| 111 |
showError($field, $field.validationMessage); |
| 112 |
} |
| 113 |
}); |
| 114 |
}); |
| 115 |
}); |
| 116 |
}); |
| 117 |
|
| 118 |
// Add event listeners. |
| 119 |
events.on(document, 'submit', '.ghostkit-form', (e) => { |
| 120 |
const $form = e.delegateTarget; |
| 121 |
|
| 122 |
// Fields validation. |
| 123 |
const isValid = $form.checkValidity(); |
| 124 |
|
| 125 |
if (!isValid) { |
| 126 |
e.preventDefault(); |
| 127 |
|
| 128 |
$form.classList.add('ghostkit-form-was-validated'); |
| 129 |
$form.querySelector(':invalid').focus(); |
| 130 |
|
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
/// Google reCaptcha. |
| 135 |
if (typeof grecaptcha !== 'undefined') { |
| 136 |
if ($form.classList.contains('ghostkit-form-processed')) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
const $recaptchaTokenField = $form.querySelector( |
| 141 |
'[name="ghostkit_form_google_recaptcha"]' |
| 142 |
); |
| 143 |
|
| 144 |
if (!$recaptchaTokenField) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
e.preventDefault(); |
| 149 |
|
| 150 |
if ($form.classList.contains('ghostkit-form-processing')) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
$form.classList.add('ghostkit-form-processing'); |
| 155 |
|
| 156 |
// Ensure Recaptcha is loaded. |
| 157 |
grecaptcha.ready(() => { |
| 158 |
grecaptcha |
| 159 |
.execute(googleReCaptchaAPISiteKey, { action: 'ghostkit' }) |
| 160 |
.then((token) => { |
| 161 |
$recaptchaTokenField.value = token; |
| 162 |
|
| 163 |
$form.classList.add('ghostkit-form-processed'); |
| 164 |
|
| 165 |
// After the token is fetched, submit the form. |
| 166 |
$form.submit(); |
| 167 |
|
| 168 |
$form.classList.remove( |
| 169 |
'ghostkit-form-processing', |
| 170 |
'ghostkit-form-processed' |
| 171 |
); |
| 172 |
}); |
| 173 |
}); |
| 174 |
} |
| 175 |
}); |
| 176 |
|
| 177 |
// Check validity on blur only for forms that were submitted and are invalid. |
| 178 |
events.on(document, 'blur', '.ghostkit-form-was-validated', (e) => { |
| 179 |
e.target.checkValidity(); |
| 180 |
}); |
| 181 |
|
| 182 |
events.on(document, 'input', '.ghostkit-form', (e) => { |
| 183 |
const $field = e.target; |
| 184 |
const valid = $field.checkValidity(); |
| 185 |
|
| 186 |
if (valid) { |
| 187 |
hideError($field); |
| 188 |
} |
| 189 |
}); |
| 190 |
|