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

Add validateOnBlur, validateOnSubmit settings and publicAPIs.showError and publicAPIs.removeError` #55

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
64 changes: 54 additions & 10 deletions dist/bouncer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
* formbouncerjs v1.4.6
* A lightweight form validation script that augments native HTML5 form validation elements and attributes.
* (c) 2019 Chris Ferdinandi
* (c) 2020 Chris Ferdinandi
* MIT License
* http://github.com/cferdinandi/bouncer
*/
Expand Down Expand Up @@ -82,6 +82,12 @@

// Form Submission
disableSubmit: false,

// Allow blur/click/input events to be opt-out
validateOnBlur: true,

// Allow validation to be turned off altogether. Useful for server-side validation use.
validateOnSubmit: true,

// Custom Events
emitEvents: true
Expand Down Expand Up @@ -536,6 +542,11 @@
}
}

// Custom message, passed directly in
if (errors.customMessage) {
return errors.customMessage;
}

// Fallback error message
return messages.fallback;

Expand Down Expand Up @@ -686,6 +697,29 @@
//
// Methods
//

/**
* Show an error message in the DOM
* @param {Node} field The field to show an error message for
* @param {Object} errors The errors on the field
* @param {Object} options Additional plugin settings
*/
publicAPIs.showError = function (field, errors, options) {
var _settings = extend(settings, options || {});

return showError(field, errors, _settings)
};

/**
* Remove an error message from the DOM
* @param {Node} field The field with the error message
* @param {Object} settings The plugin settings
*/
publicAPIs.removeError = function (field, options) {
var _settings = extend(settings, options || {});

return removeError(field, _settings);
};

/**
* Validate a field
Expand Down Expand Up @@ -797,10 +831,15 @@
publicAPIs.destroy = function () {

// Remove event listeners
document.removeEventListener('blur', blurHandler, true);
document.removeEventListener('input', inputHandler, false);
document.removeEventListener('click', inputHandler, false);
document.removeEventListener('submit', submitHandler, false);
if (settings.validateOnBlur) {
document.removeEventListener('blur', blurHandler, true);
document.removeEventListener('input', inputHandler, false);
document.removeEventListener('click', inputHandler, false);
}

if (settings.validateOnSubmit) {
document.removeEventListener('submit', submitHandler, false);
}

// Remove all errors
removeAllErrors(selector, settings);
Expand Down Expand Up @@ -832,10 +871,15 @@
addNoValidate(selector);

// Event Listeners
document.addEventListener('blur', blurHandler, true);
document.addEventListener('input', inputHandler, false);
document.addEventListener('click', inputHandler, false);
document.addEventListener('submit', submitHandler, false);
if (settings.validateOnBlur) {
document.addEventListener('blur', blurHandler, true);
document.addEventListener('input', inputHandler, false);
document.addEventListener('click', inputHandler, false);
}

if (settings.validateOnSubmit) {
document.addEventListener('submit', submitHandler, false);
}

// Emit custom event
if (settings.emitEvents) {
Expand All @@ -862,4 +906,4 @@

return Constructor;

}));
}));
Loading