Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pd-forms",
"title": "pdForms",
"description": "Customization of netteForms for use in PeckaDesign.",
"version": "4.2.0",
"version": "4.2.1",
"author": "PeckaDesign, s.r.o <support@peckadesign.cz>",
"contributors": [
"Radek Šerý <radek.sery@peckadesign.cz>",
Expand Down
42 changes: 34 additions & 8 deletions src/assets/pdForms.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @name pdForms
* @author Radek Šerý <radek.sery@peckadesign.cz>
* @version 4.2.0
* @version 4.2.1
*
* Features:
* - live validation
Expand Down Expand Up @@ -45,7 +45,7 @@

var pdForms = window.pdForms || {};

pdForms.version = '4.2.0';
pdForms.version = '4.2.1';


/**
Expand All @@ -54,7 +54,8 @@
pdForms.Nette = {
validateControl: Nette.validateControl,
toggleControl: Nette.toggleControl,
initForm: Nette.initForm
initForm: Nette.initForm,
validateForm: Nette.validateForm
};


Expand Down Expand Up @@ -619,18 +620,33 @@
}


pdForms.removeFormMessages = function(form) {
var radios = {};

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

if (elem.tagName && !(elem.tagName.toLowerCase() in {input: 1, select: 1, textarea: 1, button: 1})) {
continue;
} else if (elem.type === 'radio') {
if (radios[elem.name]) {
continue;
}
radios[elem.name] = true;
}

pdForms.removeMessages(elem, false);
}
}


/**
* Optional rules are defined using "optional" property in "arg". We have to convert arg into Nette format before
* validating. This means removing all properties but data from arg and storing them elsewhere.
*/
Nette.validateControl = function(elem, rules, onlyCheck, value, emptyOptional) {
elem = elem.tagName ? elem : elem[0]; // RadioNodeList

// assumes the input is valid, therefore removing all messages except those associated with ajax rules; this
// prevents flashing of message, when ajax rule is evaluated - ajax rules removes their messages when the ajax
// rule is evaluated
pdForms.removeMessages(elem, false);

if (! rules) {
rules = JSON.parse(elem.getAttribute('data-nette-rules') || '[]');

Expand Down Expand Up @@ -658,6 +674,16 @@
};


Nette.validateForm = function(sender, onlyCheck) {
// Assumes the form is valid, therefore removing all messages except those associated with ajax rules. This
// prevents flashing of messages when the ajax rule is evaluated - ajax rules removes their messages when the
// ajax rule is evaluated.
pdForms.removeFormMessages(sender.form || sender);

return pdForms.Nette.validateForm(sender, onlyCheck);
}


/**
* As a side effect of live validation, errors are shown directly in validateControl. This method is used only for
* focusing the first element with error.
Expand Down