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

netteForms.js: Show all error messages at once #65

Closed
wants to merge 3 commits into from
Closed
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
59 changes: 50 additions & 9 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

var Nette = {};
Nette.errors = [];

/**
* Attaches a handler to an event for the element.
Expand Down Expand Up @@ -201,11 +202,9 @@ Nette.validateForm = function(sender) {
continue;
}

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


Expand All @@ -226,15 +225,57 @@ Nette.isDisabled = function(elem) {


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


/**
* Display error messages.
*/
Nette.showErrors = function() {
var messages = [];
var focusElem;

for (var i in Nette.errors) {
var obj = Nette.errors[i];
var elem = obj.elem;
var message = obj.message;

if (messages.indexOf(message) === -1) {
messages.push(message);

if (!focusElem && elem.focus) {
focusElem = elem;
}
}
}
if (elem.focus) {
elem.focus();

Nette.errors = [];
if (messages.length) {
Nette.alert(messages.join('\n'));

if (focusElem) {
focusElem.focus();
}

return false;
}

return true;
};


/**
* Shows an alert.
*/
Nette.alert = function(message) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this while I'm working on "show all messages at once".

Tt would be nice to have an option redefine "show alert" function and create custom handlers.

alert(message);
};


Expand Down