ghinch/yui3-gallery — Issue #9: _syncFieldNode on multi choiceField only checks 1 box
Author: jindustry
Created: 2011-10-29 07:30:15+00:00
State: open

I tried to set a value for a choiceField with multi=true (set of checkboxes) like this myMultiChoiceField.set('value', 'val1,val2') and ran into a couple of problems.

First, I had to call myMultiChoiceField.syncUI() explicity. Otherwise, nothing happns. I'm not sure if that's by design.
Second, the checkboxes do not update correctly after I call syncUI. The cuprit seems to be the following. The code seems to be written with radio buttons in mind and doesn't work for checkboxes. It never marks more than 1 checkbox AND never unmarks previously marked checkboxes.

```
_syncFieldNode: function() {
var choices = this.get('value').split(',');

if (choices && choices.length > 0) {
Y.Array.each(choices, function(choice) {
this._fieldNode.each(function(node, index, list) {
if (Y.Lang.trim(node.get('value')) == Y.Lang.trim(choice)) {
node.set('checked', true);
return true; // this will never check more than 1 box.
//previously checked boxes never get unchecked
}
}, this);
}, this);
}
},
```
The following changes seem to work.

```
_syncFieldNode: function() {
var choices = this.get('value').split(',');
if (choices && choices.length > 0) {
Y.Array.each(choices, function(choice) {
choice = Y.Lang.trim(choice);
});
this._fieldNode.each(function(node, index, list) {
var nodeValue = Y.Lang.trim(node.get('value'));
if (Y.Array.indexOf(choices, nodeValue) > -1) {
node.set('checked', true);
//return true;
}
else {
node.set('checked',false);
}
}, this);
}
},
```