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 9953926 commit d1897a7
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

var Nette = {};

Nette.formErrors = [];


/**
* Attaches a handler to an event for the element.
*/
Expand Down Expand Up @@ -196,11 +199,14 @@ Nette.validateForm = function(sender) {
var form = sender.form || sender,
scope = false;

Nette.formErrors = [];

if (form['nette-submittedBy'] && form['nette-submittedBy'].getAttribute('formnovalidate') !== null) {
var scopeArr = Nette.parseJSON(form['nette-submittedBy'].getAttribute('data-nette-validation-scope'));
if (scopeArr.length) {
scope = new RegExp('^(' + scopeArr.join('-|') + '-)');
} else {
Nette.showFormErrors(form, []);
return true;
}
}
Expand All @@ -224,11 +230,13 @@ Nette.validateForm = function(sender) {
continue;
}

if (!Nette.validateControl(elem)) {
if (!Nette.validateControl(elem) && !Nette.formErrors.length) {
return false;
}
}
return true;
var success = !Nette.formErrors.length;
Nette.showFormErrors(form, Nette.formErrors);
return success;
};


Expand All @@ -249,14 +257,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.showFormErrors = function(form, 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 d1897a7

Please sign in to comment.