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 1a1e23f commit f1de028
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 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,12 @@ Nette.validateForm = function(sender) {
continue;
}

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


Expand All @@ -249,14 +256,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 f1de028

Please sign in to comment.