Skip to content

Commit

Permalink
netteForms.js: added support of array of inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 8, 2014
1 parent d410907 commit 35fc12f
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@ Nette.getValue = function(elem) {
if (!elem) {
return null;

} else if (!elem.nodeName) { // RadioNodeList
} else if (!elem.nodeName) { // RadioNodeList, HTMLCollection, array
var multi = elem[0] && !!elem[0].name.match(/\[\]$/),
res = [];

for (i = 0, len = elem.length; i < len; i++) {
if (elem[i].checked) {
if (elem[i].type in {checkbox: 1, radio: 1} && !elem[i].checked) {
continue;
} else if (multi) {
res.push(elem[i].value);
} else {
return elem[i].value;
}
}
return null;
return multi ? res : null;

} else if (!elem.form.elements[elem.name].nodeName) { // multi element
return Nette.getValue(elem.form.elements[elem.name]);

} else if (elem.nodeName.toLowerCase() === 'select') {
var index = elem.selectedIndex, options = elem.options, values = [];
Expand All @@ -51,12 +61,9 @@ Nette.getValue = function(elem) {
}
return values;

} else if (elem.type === 'checkbox') {
} else if (elem.type in {checkbox: 1, radio: 1}) {
return elem.checked;

} else if (elem.type === 'radio') {
return Nette.getValue(elem.form.elements[elem.name].nodeName ? [elem] : elem.form.elements[elem.name]);

} else if (elem.type === 'file') {
return elem.files || elem.value;

Expand Down Expand Up @@ -231,6 +238,7 @@ Nette.validateRule = function(elem, op, arg) {
Nette.validators = {
filled: function(elem, arg, val) {
return val !== '' && val !== false && val !== null
&& (!Nette.isArray(val) || val.length)
&& (!window.FileList || !(val instanceof FileList) || val.length);
},

Expand Down

0 comments on commit 35fc12f

Please sign in to comment.