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 1 commit
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
62 changes: 53 additions & 9 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

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

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

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


Expand All @@ -208,15 +207,60 @@ 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 (!focusElem && elem.focus) {
focusElem = elem;
}

if (message) {
if (messages) {
messages += '\n';
}
messages += message;
}
}
if (elem.focus) {
elem.focus();

Nette.errors = [];
if (messages) {
Nette.alert(messages);

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