diff --git a/src/assets/netteForms.js b/src/assets/netteForms.js index 42eba18a2..73c9488a1 100644 --- a/src/assets/netteForms.js +++ b/src/assets/netteForms.js @@ -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; @@ -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", ''); @@ -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; } } diff --git a/tests/netteForms/.jshintrc b/tests/netteForms/.jshintrc index 32c2bedb8..1cac83361 100644 --- a/tests/netteForms/.jshintrc +++ b/tests/netteForms/.jshintrc @@ -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. diff --git a/tests/netteForms/spec/Nette.validateRuleSpec.js b/tests/netteForms/spec/Nette.validateRuleSpec.js index b71ce3347..851821337 100644 --- a/tests/netteForms/spec/Nette.validateRuleSpec.js +++ b/tests/netteForms/spec/Nette.validateRuleSpec.js @@ -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);