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 disableValidateReadonly setting #64

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 28 additions & 13 deletions src/js/bouncer/bouncer.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@
// Form Submission
disableSubmit: false,

// Validation enable/disable
disableValidateOnBlur: false,
disableValidateOnInput: false,
disableValidateOnClick: false,
disableValidateOnSubmit: false,
disableValidateReadonly: true,

// Custom Events
emitEvents: true

Expand Down Expand Up @@ -506,12 +513,20 @@

// Numbers that are out of range
if (errors.outOfRange) {
return messages.outOfRange[errors.outOfRange].replace('{max}', field.getAttribute('max')).replace('{min}', field.getAttribute('min')).replace('{length}', field.value.length);
var message = messages.outOfRange[errors.outOfRange];
if (message instanceof String) {
message = message.replace('{max}', field.getAttribute('max')).replace('{min}', field.getAttribute('min')).replace('{length}', field.value.length);
}
return message;
}

// Values that are too long or short
if (errors.wrongLength) {
return messages.wrongLength[errors.wrongLength].replace('{maxLength}', field.getAttribute('maxlength')).replace('{minLength}', field.getAttribute('minlength')).replace('{length}', field.value.length);
var message = messages.wrongLength[errors.wrongLength];
if (message instanceof String) {
message = message.replace('{maxLength}', field.getAttribute('maxlength')).replace('{minLength}', field.getAttribute('minlength')).replace('{length}', field.value.length);
}
return message;
}

// Pattern mismatch error
Expand Down Expand Up @@ -687,12 +702,12 @@
*/
publicAPIs.validate = function (field, options) {

// Don't validate submits, buttons, file and reset inputs, and disabled and readonly fields
if (field.disabled || field.readOnly || field.type === 'reset' || field.type === 'submit' || field.type === 'button') return;

// Local settings
var _settings = extend(settings, options || {});

// Don't validate submits, buttons, file and reset inputs, and disabled and readonly fields
if (field.disabled || (field.readOnly && _settings.disableValidateReadonly) || field.type === 'reset' || field.type === 'submit' || field.type === 'button') return;

// Check for errors
var isValid = getErrors(field, _settings);

Expand Down Expand Up @@ -789,10 +804,10 @@
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.disableValidateOnBlur) document.removeEventListener('blur', blurHandler, true);
if (! settings.disableValidateOnInput) document.removeEventListener('input', inputHandler, false);
if (! settings.disableValidateOnClick) document.removeEventListener('click', inputHandler, false);
if (! settings.disableValidateOnSubmit) document.removeEventListener('submit', submitHandler, false);

// Remove all errors
removeAllErrors(selector, settings);
Expand Down Expand Up @@ -824,10 +839,10 @@
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.disableValidateOnBlur) document.addEventListener('blur', blurHandler, true);
if (! settings.disableValidateOnInput) document.addEventListener('input', inputHandler, false);
if (! settings.disableValidateOnClick) document.addEventListener('click', inputHandler, false);
if (! settings.disableValidateOnSubmit) document.addEventListener('submit', submitHandler, false);

// Emit custom event
if (settings.emitEvents) {
Expand Down