-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix required form radio/checkbox fields (#1643)
* Fix required form radio/checkbox fields * Shift scripts to it's own JS file. Update markup. Tweaks. * add inline docblock * Add test to check for loaded script * Fix incorrect radio field values being passed to the emails * Tweak error notice * Fix test_render_field_radio unit test * Update docs with info on coblocks_form_checkbox_required_text filter * Fix test_required_checkbox_script test * Remove jQuery dependency, rewrite everything with vanilla JS * Update unit tests * adjust docs wording Co-authored-by: Rich Tabor <hi@richtabor.com> Co-authored-by: AnthonyLedesma <anthonymledesma@gmail.com>
- Loading branch information
1 parent
f4f1f45
commit 19426b2
Showing
7 changed files
with
136 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
document.addEventListener( 'DOMContentLoaded', function() { | ||
document.querySelectorAll( '.coblocks-form form' ).forEach( form => { | ||
|
||
let requiredErrorDiv = form.getElementsByClassName( 'required-error' )[0]; | ||
|
||
// No required checkboxes | ||
if ( ! form.querySelectorAll( '.coblocks-field.checkbox.required' ).length ) { | ||
return; | ||
} | ||
|
||
// Form Submit Event Listener | ||
form.addEventListener( 'submit', event => { | ||
let selectedCheckboxes = form.querySelectorAll( '.coblocks-field.checkbox.required input[type="checkbox"]:checked' ).length; | ||
if ( selectedCheckboxes === 0 ) { | ||
requiredErrorDiv.style.display = 'block'; | ||
event.preventDefault(); | ||
return; | ||
} | ||
requiredErrorDiv.style.display = 'none'; | ||
} ); | ||
|
||
// Required Checkbox Event Listener | ||
form.querySelectorAll( '.coblocks-field.checkbox.required input[type="checkbox"]' ).forEach( requiredCheckbox => { | ||
requiredCheckbox.addEventListener( 'change', () => { | ||
requiredErrorDiv.style.display = 'none'; | ||
} ); | ||
} ); | ||
|
||
} ); | ||
} ); |