Skip to content

Instantly share code, notes, and snippets.

@vojtech-dobes
Last active January 4, 2016 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vojtech-dobes/8575265 to your computer and use it in GitHub Desktop.
Save vojtech-dobes/8575265 to your computer and use it in GitHub Desktop.
Keeps form buttons with [data-button-validated] data attribute in disabled state, until the form passes validation. Extension for nette.ajax.js.
$.nette.ext('validatedButton', {
load: function () {
var that = this;
$('[data-button-validated]').each(function () {
var button = $(this);
var form = button.closest('form');
that.tryEnable(form, button);
form.find('input, select').on('change input', function () {
that.tryEnable(form, button);
});
});
}
}, {
bypassAlert: function () {
var originalAlert = Nette.addError;
Nette.addError = function () {};
return function () {
Nette.addError = originalAlert;
};
},
enable: function (button) {
button.removeAttr('disabled');
},
disable: function (button) {
button.attr('disabled', true);
},
tryEnable: function (form, button) {
var restoreAlert = this.bypassAlert();
var result = Nette.validateForm(form.get(0));
if (result) {
this.enable(button);
} else {
this.disable(button);
}
restoreAlert();
return result;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment