Skip to content

Commit

Permalink
netteForms.js: fixed length validation on checkbox list with exactly …
Browse files Browse the repository at this point in the history
…1 item [Closes #59]
  • Loading branch information
dg committed Feb 8, 2015
1 parent c469a59 commit ac5dacc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ Nette.getValue = function(elem) {
} else if (!elem.form.elements[elem.name].nodeName) { // multi element
return Nette.getValue(elem.form.elements[elem.name]);

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

} else if (elem.name.match(/\[\]$/)) { // multi element with single option
return Nette.getValue([elem]);

} else if (elem.nodeName.toLowerCase() === 'select') {
var index = elem.selectedIndex, options = elem.options, values = [];

Expand All @@ -67,9 +73,6 @@ Nette.getValue = function(elem) {
} else if (elem.type === 'radio') {
return elem.checked && elem.value;

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

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

Expand Down
23 changes: 23 additions & 0 deletions tests/netteForms.js/spec/Nette.validateRuleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,29 @@ describe('Nette.getValue & validateRule', function() {
});


it('checkbox list with single item', function() {
fixtures.load('checkboxList-single.html');
var doc = fixtures.window().document,
form = doc.forms[0],
el = form['input[]'];

expect(Nette.getValue(el)).toEqual([]);
expect(Nette.validateRule(el, 'filled')).toBe(false);
expect(Nette.validateRule(el, 'blank')).toBe(true);
expect(Nette.validateRule(el, 'equal', ['r', 'g', 'b'])).toBe(true);

doc.getElementById('input-r').checked = true;
expect(Nette.getValue(el)).toEqual(['r']);
expect(Nette.validateRule(el, 'filled')).toBe(true);
expect(Nette.validateRule(el, 'blank')).toBe(false);
expect(Nette.validateRule(el, 'equal', 'r')).toBe(true);
expect(Nette.validateRule(el, 'equal', 'g')).toBe(false);
expect(Nette.validateRule(el, 'equal', ['r', 'g'])).toBe(true);
expect(Nette.validateRule(el, 'minLength', 1)).toBe(true);
expect(Nette.validateRule(el, 'minLength', 2)).toBe(false);
});


it('radio', function() {
fixtures.load('radio.html');
var doc = fixtures.window().document,
Expand Down

0 comments on commit ac5dacc

Please sign in to comment.