Skip to content

Commit

Permalink
netteForms.js: fixed getValue for radio list [Closes #73]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 6, 2015
1 parent cde467a commit 089620c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
44 changes: 21 additions & 23 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,21 @@ Nette.addEvent = function(element, on, callback) {
* Returns the value of form element.
*/
Nette.getValue = function(elem) {
var i, len;
var i;
if (!elem) {
return null;

} else if (!elem.tagName) { // RadioNodeList, HTMLCollection, array
var multi = elem[0] && !!elem[0].name.match(/\[\]$/),
res = [];
return elem[0] ? Nette.getValue(elem[0]) : null;

for (i = 0, len = elem.length; i < len; i++) {
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;
} else if (elem.type === 'radio') {
var elements = elem.form.elements; // prevents problem with name 'item' or 'namedItem'
for (i = 0; i < elements.length; i++) {
if (elements[i].name === elem.name && elements[i].checked) {
return elements[i].value;
}
}
return multi ? res : null;

} else if (elem.name && !elem.form.elements.namedItem(elem.name).tagName) { // multi element
return Nette.getValue(elem.form.elements.namedItem(elem.name));
return null;

} else if (elem.type === 'file') {
return elem.files || elem.value;
Expand All @@ -75,22 +69,27 @@ Nette.getValue = function(elem) {
return index < 0 ? null : options[index].value;
}

for (i = 0, len = options.length; i < len; i++) {
for (i = 0; i < options.length; i++) {
if (options[i].selected) {
values.push(options[i].value);
}
}
return values;

} else if (elem.name && elem.name.match(/\[\]$/)) { // multi element with single option
return Nette.getValue([elem]);
} else if (elem.name && elem.name.match(/\[\]$/)) { // multiple elements []
var elements = elem.form.elements[elem.name].tagName ? [elem] : elem.form.elements[elem.name],
values = [];

for (i = 0; i < elements.length; i++) {
if (elements[i].type !== 'checkbox' || elements[i].checked) {
values.push(elements[i].value);
}
}
return values;

} else if (elem.type === 'checkbox') {
return elem.checked;

} else if (elem.type === 'radio') {
return elem.checked && elem.value;

} else if (elem.tagName.toLowerCase() === 'textarea') {
return elem.value.replace("\r", '');

Expand Down Expand Up @@ -215,9 +214,8 @@ Nette.validateForm = function(sender) {
*/
Nette.isDisabled = function(elem) {
if (elem.type === 'radio') {
elem = elem.form.elements.namedItem(elem.name).tagName ? [elem] : elem.form.elements.namedItem(elem.name);
for (var i = 0; i < elem.length; i++) {
if (!elem[i].disabled) {
for (var i = 0, elements = elem.form.elements; i < elements.length; i++) {
if (elements[i].name === elem.name && !elements[i].disabled) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/netteForms/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"regexp" : true, // Prohibit `.` and `[^...]` in regular expressions.
"regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`.
"scripturl" : true, // Tolerate script-targeted URLs.
"shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
"shadow" : true, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
"supernew" : false, // Tolerate `new function () { ... };` and `new Object;`.
"undef" : true, // Require all non-global variables be declared before they are used.

Expand Down
2 changes: 1 addition & 1 deletion tests/netteForms/spec/Nette.validateRuleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('Nette.getValue & validateRule', function() {
form = doc.forms[0],
el = form.input;

expect(Nette.getValue(el)).toBe(false);
expect(Nette.getValue(el)).toBe(null);
expect(Nette.validateRule(el, 'filled')).toBe(false);
expect(Nette.validateRule(el, 'blank')).toBe(true);
expect(Nette.validateRule(el, 'equal', ['f', 'm'])).toBe(false);
Expand Down

0 comments on commit 089620c

Please sign in to comment.