If you need multiple checkboxes instead of list in CakePHP 1.1 you
could use code from
http://bakery.cakephp.org/articles/view/create-multiple-checkboxes-instead-of-a-multiple-select-in-your-views
This is the version for CakePHP 1.2
Example of use in the view
<?=$habtm->checkboxMultiple('Tag', $tags, Set::extract($this-
>data['Tag'], '{n}.Tag.id'))?>
<?php
class HabtmHelper extends HtmlHelper {
/**
* Returns a list of checkboxes.
*
* @param string $fieldName Name attribute of the SELECT
* @param array $options Array of the elements (as 'value'=>'Text'
pairs)
* @param array $selected Selected checkboxes
* @param string $inbetween String that separates the checkboxes.
* @param array $htmlAttributes Array of HTML options
* @param boolean $return Whether this method should
return a value
* @return string List of checkboxes
*/
function checkboxMultiple($fieldName, $options, $selected = null,
$inbetween = null, $htmlAttributes = null, $return = false) {
$this->tags['checkboxmultiple'] = '<input type="checkbox"
name="data[%s][%s][]" %s/>%s';
$this->tags['hiddenmultiple'] = '<input type="hidden"
name="data[%s]
[%s][]" %s/>';
$this->setFormTag($fieldName);
if ($this->tagIsInvalid($this->model(), $this->field())) {
if (isset($htmlAttributes['class']) &&
trim($htmlAttributes['class']) != "") {
$htmlAttributes['class'] .= ' form_error';
} else {
$htmlAttributes['class'] = 'form_error';
}
}
if (!is_array($options)) {
return null;
}
if (!isset($selected)) {
$selected = $this->value(null, $fieldName);
}
foreach($options as $name => $title) {
$optionsHere = $htmlAttributes;
if (($selected !== null) && ($selected == $name)) {
$optionsHere['checked'] = 'checked';
} else if (is_array($selected) && array_key_exists($name,
$selected)) {
$optionsHere['checked'] = 'checked';
}
$optionsHere['value'] = $name;
$checkbox[] = "<li>" . sprintf($this-
>tags['checkboxmultiple'], $this->model(), $this->field(), $this-
>_parseAttributes($optionsHere), $title) . "</li>\n";
}
return "\n" . sprintf($this->tags['hiddenmultiple'], $this-
>model(), $this->field(), null, $title) . "\n<ul class=
\"checkboxMultiple\">\n" . $this->output(implode($checkbox),
$return) . "</ul>\n";
}
}
?>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---