Skip to content

Commit

Permalink
netteForms.js: ability to show all error messages at once via Nette.s…
Browse files Browse the repository at this point in the history
…howErrors() [Closes #65]
  • Loading branch information
hranicka authored and dg committed Jun 30, 2016
1 parent 5516648 commit a357520
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/assets/netteForms.js
Expand Up @@ -30,6 +30,9 @@

var Nette = {};

Nette.formErrors = [];


/**
* Attaches a handler to an event for the element.
*/
Expand Down Expand Up @@ -207,6 +210,7 @@ Nette.validateForm = function(sender) {

var radios = {}, i, elem;

Nette.formErrors = [];
for (i = 0; i < form.elements.length; i++) {
elem = form.elements[i];

Expand All @@ -224,11 +228,12 @@ Nette.validateForm = function(sender) {
continue;
}

if (!Nette.validateControl(elem)) {
if (!Nette.validateControl(elem) && !Nette.formErrors.length) {
return false;
}
}
return true;
Nette.showErrors(Nette.formErrors);
return !Nette.formErrors.length;
};


Expand All @@ -249,14 +254,29 @@ Nette.isDisabled = function(elem) {


/**
* Display error message.
* Adds error message to the queue.
*/
Nette.addError = function(elem, message) {
if (message) {
alert(message);
Nette.formErrors.push({
element: elem,
message: message
});
};


/**
* Display error messages.
*/
Nette.showErrors = function(errors) {
if (!errors.length) {
return;
}
var error = errors[0];
if (error.message) {
alert(error.message);
}
if (elem.focus) {
elem.focus();
if (error.element.focus) {
error.element.focus();
}
};

Expand Down

0 comments on commit a357520

Please sign in to comment.