Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to Vanilla JS validation #16

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion css/bs-cf7-style.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 107 additions & 1 deletion js/bs-cf7-script.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,110 @@
* bS Contact Form 7
*
* @version 5.2.0
*/ document.addEventListener("wpcf7invalid",function(e){e.target.querySelector(".wpcf7-response-output").classList.add("alert","alert-danger")},!1),document.addEventListener("wpcf7spam",function(e){var t=e.target.querySelector(".wpcf7-response-output");t.classList.remove("alert-danger"),t.classList.add("alert","alert-warning")},!1),document.addEventListener("wpcf7mailfailed",function(e){var t=e.target.querySelector(".wpcf7-response-output");t.classList.remove("alert-danger"),t.classList.add("alert","alert-warning")},!1),document.addEventListener("wpcf7mailsent",function(e){var t=e.target,r=t.querySelector(".wpcf7-response-output");r.classList.remove("alert-danger","alert-warning"),r.classList.add("alert","alert-success"),t.querySelector("button.wpcf7-submit").disabled=!0},!1);var submitButtons=document.querySelectorAll(".wpcf7-submit");submitButtons.forEach(function(e){var t=document.createElement("div");t.classList.add("ajax-loader");var r=document.createElement("div");r.classList.add("spinner-border","spinner-border-sm"),r.setAttribute("role","status"),r.setAttribute("aria-hidden","true"),t.appendChild(r),e.insertBefore(t,e.firstChild)});
*/

// Validation Events for changing response CSS classes
document.addEventListener('wpcf7invalid', function (event) {
var form = event.target; // Get the form that triggered the event
var responseOutput = form.querySelector('.wpcf7-response-output');
responseOutput.classList.add('alert', 'alert-danger');
}, false);

document.addEventListener('wpcf7spam', function (event) {
var form = event.target; // Get the form that triggered the event
var responseOutput = form.querySelector('.wpcf7-response-output');
responseOutput.classList.remove('alert-danger');
responseOutput.classList.add('alert', 'alert-warning');
}, false);

document.addEventListener('wpcf7mailfailed', function (event) {
var form = event.target; // Get the form that triggered the event
var responseOutput = form.querySelector('.wpcf7-response-output');
responseOutput.classList.remove('alert-danger');
responseOutput.classList.add('alert', 'alert-warning');
}, false);

document.addEventListener('wpcf7mailsent', function (event) {
var form = event.target; // Get the form that triggered the event
var responseOutput = form.querySelector('.wpcf7-response-output');
responseOutput.classList.remove('alert-danger', 'alert-warning');
responseOutput.classList.add('alert', 'alert-success');

// Disable submit button
var submitButton = form.querySelector('button.wpcf7-submit');
submitButton.disabled = true;
}, false);


// Spinner to submit button when submitting

// [submit class:btn class:btn-primary class:w-100 "Send Message"] results in <input>
// <input class="wpcf7-form-control wpcf7-submit btn btn-primary w-100" type="submit" value="Send Message" disabled="">

// Use <button> instead shortcode in forms HTML to add a spinner
// <button type="submit" class="btn btn-primary wpcf7-submit w-100" disabled="disabled">Send Message</button>

var submitButtons = document.querySelectorAll('.wpcf7-submit');

submitButtons.forEach(function(button) {
var loaderDiv = document.createElement('div');
loaderDiv.classList.add('ajax-loader');

var spinnerDiv = document.createElement('div');
spinnerDiv.classList.add('spinner-border', 'spinner-border-sm');
spinnerDiv.setAttribute('role', 'status');
spinnerDiv.setAttribute('aria-hidden', 'true');

loaderDiv.appendChild(spinnerDiv);

button.insertBefore(loaderDiv, button.firstChild);
});


// Validation
document.addEventListener('DOMContentLoaded', function() {
// Select all forms with the 'wpcf7-form' class
var forms = document.querySelectorAll('.wpcf7-form');

// Iterate over each form
forms.forEach(function(form) {
// Add the 'needs-validation' class to the form
form.classList.add('needs-validation');

// Add the 'novalidate' attribute to disable default browser validation
form.setAttribute('novalidate', '');

// Find fields with 'wpcf7-validates-as-required' class and set 'required' attribute
var requiredFields = form.querySelectorAll('.wpcf7-validates-as-required');
requiredFields.forEach(function(field) {
field.setAttribute('required', '');
});

// Find acceptance checkboxes excluding those with class 'optional' and set 'required' attribute
var acceptanceCheckboxes = form.querySelectorAll('.wpcf7-acceptance:not(.optional) .form-check-input');
acceptanceCheckboxes.forEach(function(checkbox) {
checkbox.setAttribute('required', '');
});

// Find checkboxes within '.wpcf7-checkbox' and exclude those with class 'form-check-input' to make them optional
var optionalCheckboxes = form.querySelectorAll('.wpcf7-checkbox:not(.wpcf7-exclusive-checkbox) .form-check-input');
optionalCheckboxes.forEach(function(checkbox) {
checkbox.removeAttribute('required');
});

// Find checkboxes within '.wpcf7-exclusive-checkbox' and set 'required' attribute
var exclusiveCheckboxes = form.querySelectorAll('.wpcf7-exclusive-checkbox .form-check-input');
exclusiveCheckboxes.forEach(function(checkbox) {
checkbox.setAttribute('required', '');
});

// Prevent form submission and display validation styles
form.addEventListener('submit', function(event) {
if (!form.checkValidity()) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
});
});
});
49 changes: 49 additions & 0 deletions src/bs-cf7-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,52 @@ submitButtons.forEach(function(button) {

button.insertBefore(loaderDiv, button.firstChild);
});


// Validation
document.addEventListener('DOMContentLoaded', function() {
// Select all forms with the 'wpcf7-form' class
var forms = document.querySelectorAll('.wpcf7-form');

// Iterate over each form
forms.forEach(function(form) {
// Add the 'needs-validation' class to the form
form.classList.add('needs-validation');

// Add the 'novalidate' attribute to disable default browser validation
form.setAttribute('novalidate', '');

// Find fields with 'wpcf7-validates-as-required' class and set 'required' attribute
var requiredFields = form.querySelectorAll('.wpcf7-validates-as-required');
requiredFields.forEach(function(field) {
field.setAttribute('required', '');
});

// Find acceptance checkboxes excluding those with class 'optional' and set 'required' attribute
var acceptanceCheckboxes = form.querySelectorAll('.wpcf7-acceptance:not(.optional) .form-check-input');
acceptanceCheckboxes.forEach(function(checkbox) {
checkbox.setAttribute('required', '');
});

// Find checkboxes within '.wpcf7-checkbox' and exclude those with class 'form-check-input' to make them optional
var optionalCheckboxes = form.querySelectorAll('.wpcf7-checkbox:not(.wpcf7-exclusive-checkbox) .form-check-input');
optionalCheckboxes.forEach(function(checkbox) {
checkbox.removeAttribute('required');
});

// Find checkboxes within '.wpcf7-exclusive-checkbox' and set 'required' attribute
var exclusiveCheckboxes = form.querySelectorAll('.wpcf7-exclusive-checkbox .form-check-input');
exclusiveCheckboxes.forEach(function(checkbox) {
checkbox.setAttribute('required', '');
});

// Prevent form submission and display validation styles
form.addEventListener('submit', function(event) {
if (!form.checkValidity()) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
});
});
});
171 changes: 0 additions & 171 deletions src/bs-cf7-style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,174 +50,3 @@
button.btn.btn-primary.wpcf7-submit:disabled {
cursor: not-allowed;
}


// Validation

// Forms

// Valid on submitted
.wpcf7-form.invalid {

.form-control,
.form-select {
border-color: var(--#{$prefix}form-valid-border-color);
background-repeat: no-repeat;

&:focus {
box-shadow: 0 0 $input-btn-focus-blur $input-focus-width rgba(var(--#{$prefix}success-rgb), $input-btn-focus-color-opacity);
}
}

.form-control {
background-image: var(--valid-icon);
padding-right: $input-height-inner;
background-position: right $input-height-inner-quarter center;
background-size: $input-height-inner-half $input-height-inner-half;
}

textarea.form-control {
background-position: top $input-height-inner-quarter right $input-height-inner-quarter;
}

.form-select {
--bs-form-select-bg-icon: var(--valid-icon);
padding-right: $form-select-feedback-icon-padding-end;
background-position: $form-select-bg-position, $form-select-feedback-icon-position;
background-size: $form-select-bg-size, $form-select-feedback-icon-size;
}

// Checks & Radios

.form-check-checkbox,
.form-check-radio {
color: var(--#{$prefix}form-valid-color);
}

.form-check-input {
border-color: var(--#{$prefix}form-valid-border-color);

&:checked {
background-color: var(--#{$prefix}form-valid-color);
}

&:focus {
border-color: var(--#{$prefix}form-valid-border-color);
box-shadow: 0 0 $input-btn-focus-blur $input-focus-width rgba(var(--#{$prefix}success-rgb), $input-btn-focus-color-opacity) !important;
}
}
}


// Invalid live validation

.wpcf7-not-valid {
.form-check-checkbox {
color: var(--#{$prefix}form-invalid-color);
}

.form-check-input {
border-color: var(--#{$prefix}form-invalid-border-color);

&:focus {
border-color: var(--#{$prefix}form-invalid-border-color);
box-shadow: 0 0 $input-btn-focus-blur $input-focus-width rgba(var(--#{$prefix}danger-rgb), $input-btn-focus-color-opacity) !important;
}
}

&.form-select {
--bs-form-select-bg-icon: var(--invalid-icon) !important;
padding-right: $form-select-feedback-icon-padding-end;
background-position: $form-select-bg-position, $form-select-feedback-icon-position;
background-size: $form-select-bg-size, $form-select-feedback-icon-size;
}
}

.wpcf7-form.invalid {
.wpcf7-not-valid {
.form-check-checkbox {
color: var(--#{$prefix}form-invalid-color);
}

.form-check-input {
border-color: var(--#{$prefix}form-invalid-border-color);

&:focus {
border-color: var(--#{$prefix}form-invalid-border-color);
box-shadow: 0 0 $input-btn-focus-blur $input-focus-width rgba(var(--#{$prefix}danger-rgb), $input-btn-focus-color-opacity) !important;
}
}
}
}


// Acceptance
.wpcf7-form.invalid {

.wpcf7-acceptance:not(.wpcf7-acceptance.optional) {

// Valid

.form-check-input {

&:checked {
border-color: var(--#{$prefix}form-valid-border-color);
}

&:checked:focus {
box-shadow: 0 0 $input-btn-focus-blur $input-focus-width rgba(var(--#{$prefix}success-rgb), $input-btn-focus-color-opacity) !important;
}
}

&:has(.form-check-input:checked) .form-check-label {
color: var(--#{$prefix}form-valid-color);
}

// Invalid

.form-check-input {
border-color: var(--#{$prefix}form-invalid-border-color);

&:focus {
box-shadow: 0 0 $input-btn-focus-blur $input-focus-width rgba(var(--#{$prefix}danger-rgb), $input-btn-focus-color-opacity) !important;
}
}

&:has(.form-check-input) .form-check-label {
color: var(--#{$prefix}form-invalid-color);
}
}
}

// Invalid fields

.wpcf7-not-valid {

&.form-control,
&.form-select {
border-color: var(--#{$prefix}form-invalid-border-color) !important;
background-repeat: no-repeat;

&:focus {
box-shadow: 0 0 $input-btn-focus-blur $input-focus-width rgba(var(--#{$prefix}danger-rgb), $input-btn-focus-color-opacity) !important;
}
}

&.form-control {
background-image: var(--invalid-icon) !important;
padding-right: $input-height-inner;
background-position: right $input-height-inner-quarter center;
background-size: $input-height-inner-half $input-height-inner-half;
}

.form-select {
--bs-form-select-bg-icon: var(--invalid-icon) !important;
padding-right: $form-select-feedback-icon-padding-end;
background-position: $form-select-bg-position, $form-select-feedback-icon-position;
background-size: $form-select-bg-size, $form-select-feedback-icon-size;
}
}

textarea.wpcf7-not-valid.form-control {
background-position: top $input-height-inner-quarter right $input-height-inner-quarter;
}