Commit 3ddea2a4 3ddea2a4b948a134020315bbcb130083ad9f7b71 by Nicolas Perriault

refs #259 - refactored 7c2137eb

1 parent 7c2137eb
...@@ -415,46 +415,44 @@ ...@@ -415,46 +415,44 @@
415 * @return Mixed 415 * @return Mixed
416 */ 416 */
417 this.getFieldValue = function getFieldValue(inputName) { 417 this.getFieldValue = function getFieldValue(inputName) {
418 function getSingleValue(input) {
419 try {
420 type = input.getAttribute('type').toLowerCase();
421 } catch (e) {
422 type = 'other';
423 }
424 if (['checkbox', 'radio'].indexOf(type) === -1) {
425 return input.value;
426 }
427 // single checkbox or… radio button (weird, I know)
428 if (input.hasAttribute('value')) {
429 return input.checked ? input.getAttribute('value') : undefined;
430 }
431 return input.checked;
432 }
433 function getMultipleValues(inputs) {
434 type = inputs[0].getAttribute('type').toLowerCase();
435 if (type === 'radio') {
436 var value;
437 [].forEach.call(inputs, function(radio) {
438 value = radio.checked ? radio.value : undefined;
439 });
440 return value;
441 } else if (type === 'checkbox') {
442 var values = [];
443 [].forEach.call(inputs, function(checkbox) {
444 if (checkbox.checked) {
445 values.push(checkbox.value);
446 }
447 });
448 return values;
449 }
450 }
418 var inputs = this.findAll('[name="' + inputName + '"]'), type; 451 var inputs = this.findAll('[name="' + inputName + '"]'), type;
419 switch (inputs.length) { 452 switch (inputs.length) {
420 case 0: 453 case 0: return null;
421 return null; 454 case 1: return getSingleValue(inputs[0]);
422 case 1: 455 default: return getMultipleValues(inputs);
423 //this.log(inputs[0].nodeName.toLowerCase(), "error");
424 var input = inputs[0];
425 try {
426 type = input.getAttribute('type').toLowerCase();
427 } catch (e) {
428 type = 'other';
429 }
430 if (['checkbox', 'radio'].indexOf(type) === -1) {
431 return input.value;
432 }
433 // single checkbox or… radio button (weird, I know)
434 if (input.hasAttribute('value')) {
435 return input.checked ? input.getAttribute('value') : undefined;
436 } else {
437 return input.checked;
438 }
439 break;
440 default:
441 type = inputs[0].getAttribute('type').toLowerCase();
442 if (type === 'radio') {
443 var value;
444 [].forEach.call(inputs, function(radio) {
445 value = radio.checked ? radio.value : undefined;
446 });
447 return value;
448 } else if (type === 'checkbox') {
449 var values = [];
450 [].forEach.call(inputs, function(checkbox) {
451 if (checkbox.checked) {
452 values.push(checkbox.value);
453 }
454 });
455 return values;
456 }
457 break;
458 } 456 }
459 }; 457 };
460 458
......