Usage
applyValidationClasses(element, validation, showFeedback)
Parameters
element
(Required) (Object) The jQuery object of the input element itself
Default: None
validation
(Optional) (String) The type of validation to apply
Default: None
showFeedback
(Optional) (Boolean) Whether to show the form-control-feedback element
Default: false
Parameter Notes
Options for the validation parameter are “valid”, “invalid”, or “reset”. Reset simply removes all validation classes.
Examples
Add valid classes without showing feedback.
applyValidationClasses(jQuery(this), 'valid', false);
Add invalid classes and show feedback (if it exists).
applyValidationClasses(jQuery(this), 'invalid', true);
Show feedback without altering validation classes.
applyValidationClasses(jQuery(this), false, true);
Hide feedback without altering validation classes.
applyValidationClasses(jQuery(this), false, false);
Shortcut to show feedback without altering validation classes.
applyValidationClasses(jQuery(this), 'feedback');
Demo
Additional Notes
This function also removes Contact Form 7 validation classes (because CF7 classes are applied on submit of a form).
Sidenote: To validate required select menus, be sure the first option has value="".
Sidenote: Checkbox and radio validation in Bootstrap 4 only works with customized elements and as such is not included with Nebula live validation.
Source File
Located in /assets/js/modules/forms.js on line 730.
No Hooks
This function does not have any filters or actions available. Request one?nebula.applyValidationClasses = function($element, validation, showFeedback){
if ( typeof $element === 'string' ){
$element = jQuery($element);
} else if ( typeof $element !== 'object' ){
return false;
}
if ( validation === 'success' || validation === 'valid' ){
$element.removeClass('wpcf7-not-valid is-invalid').addClass('is-valid').parent().find('.wpcf7-not-valid-tip').remove();
} else if ( validation === 'danger' || validation === 'error' || validation === 'invalid' ){
$element.removeClass('wpcf7-not-valid is-valid').addClass('is-invalid');
} else if ( validation === 'reset' || validation === 'remove' ){
$element.removeClass('wpcf7-not-valid is-invalid is-valid').parent().find('.wpcf7-not-valid-tip').remove();
}
//Find the invalid feedback element (if it exists)
let parentElement = $element.parent();
let feedbackElement = false;
if ( $element.parent().find('.invalid-feedback').length ){
parentElement = $element.parent();
feedbackElement = $element.parent().find('.invalid-feedback');
} else if ( $element.closest('.form-group, .form-check').find('.invalid-feedback').length ){
parentElement = $element.closest('.form-group, .form-check');
feedbackElement = $element.closest('.form-group, .form-check').find('.invalid-feedback');
} else if ( $element.parents('.nebula-form-group').find('.invalid-feedback').length ){
parentElement = $element.parents('.nebula-form-group');
feedbackElement = $element.parents('.nebula-form-group').find('.invalid-feedback');
}
if ( feedbackElement ){
if ( validation === 'feedback' || showFeedback ){
feedbackElement.removeClass('hidden').show();
if ( parentElement.find('.wpcf7-not-valid-tip').length ){
parentElement.find('.wpcf7-not-valid-tip').addClass('hidden'); //Hide the default CF7 message if we have a more helpful one for this field
}
} else {
feedbackElement.addClass('hidden').hide();
}
}
};
Override
To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).
For non-module import functions:
nebula.applyValidationClasses = function(element, validation, showFeedback){
//Write your own code here, leave it blank, or return false.
}
For dynamically imported module function overrides:
jQuery(window).on('load', function(){
nebula.applyValidationClasses = function(element, validation, showFeedback){
//Write your own code here, leave it blank, or return false.
}
});
Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:
jQuery(document).on('nebula_module_loaded', function(module){
//Note that the module variable is also available to know which module specifically was imported
if ( typeof nebula.applyValidationClasses === 'function' ){
nebula.applyValidationClasses = function(element, validation, showFeedback){
//Write your own code here, leave it blank, or return false.
}
}
});