Somewhat answering my own question but also to get some feedback: Here
is my work in progress widget solution:
class MultipleCheckboxInput extends FormWidget {
function render(FormField $field, $data, $attributes = array()) {
$choices = $field->choices();
$index = array();
if(is_array($data)) {
foreach($data as $checked) {
$index[$checked] = array_key_exists($checked,
$choices);
}
}
$html = '<ul>';
foreach ($field->choices() as $value => $label) {
$html .= sprintf('<li><label for="%s"><input
type="checkbox" name="%s[]" id="%s" value="%s" %s /> %s</label></li>',
html_escape($field->name()) .
'_' .html_escape($value),
html_escape($field->name()),
html_escape($field->name()) .
'_' .html_escape($value),
html_escape($value),
!empty($index) && isset($index[$value]) ?
'checked="checked" ' : '',
html_escape($label)
);
}
return $html . '</ul>';
}
public function extract($field, $data) {
$values = $data->get($field->name());
$choices = $field->choices();
$index = array();
if(is_array($values)) {
foreach($values as $value) {
$index[$value] = array_key_exists($value, $choices);
}
}
if (!empty($index)) {
return $index;
}
return $values;
}
}
It looks like I'm going to have to make a MultipleChoiceField
FormField class as well as ChoiceField's clean() method doesn't allow
for multiple choices (ie $value = array() ):
public function clean($value) {
if ($this->isRequired() && !in_array($value, array_keys($this-
>choices)))
throw new FormValidationException($this-
>error_messages['invalid_choice']);
return $value;
}
Nick