Adding 'multiple' => 'checkbox' option to FormHelper::select() (Ticket #1901 and Ticket #3043)

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5992 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-11-14 05:51:49 +00:00
parent f5dc3c7655
commit 1f56ef2b7a
2 changed files with 45 additions and 8 deletions

View file

@ -985,8 +985,11 @@ class FormHelper extends AppHelper {
* @return string Formatted SELECT element
*/
function select($fieldName, $options = array(), $selected = null, $attributes = array(), $showEmpty = '') {
$select = array();
$showParents = false;
$escapeOptions = true;
$style = null;
$tag = null;
if (isset($attributes['escape'])) {
$escapeOptions = $attributes['escape'];
@ -1012,12 +1015,22 @@ class FormHelper extends AppHelper {
}
if (isset($attributes) && array_key_exists('multiple', $attributes)) {
if ($attributes['multiple'] === 'checkbox') {
if (isset($this->Html->tags['checkboxmultiplestart'])) {
$tag = $this->Html->tags['checkboxmultiplestart'];
}
$style = 'checkbox';
} else {
$tag = $this->Html->tags['selectmultiplestart'];
}
} else {
$tag = $this->Html->tags['selectstart'];
$this->__secure();
}
if (!empty($tag)) {
$select[] = sprintf($tag, $attributes['name'], $this->_parseAttributes($attributes, array('name', 'value')));
}
if ($showEmpty !== null && $showEmpty !== false && !(empty($showEmpty) && (isset($attributes) && array_key_exists('multiple', $attributes)))) {
if ($showEmpty === true) {
@ -1027,8 +1040,16 @@ class FormHelper extends AppHelper {
$options[''] = $showEmpty;
$options = array_reverse($options, true);
}
$select = am($select, $this->__selectOptions(array_reverse($options, true), $selected, array(), $showParents, array('escape' => $escapeOptions)));
$select[] = sprintf($this->Html->tags['selectend']);
$select = am($select, $this->__selectOptions(array_reverse($options, true), $selected, array(), $showParents, array('escape' => $escapeOptions, 'style' => $style)));
if ($style == 'checkbox') {
if (isset($this->Html->tags['checkboxmultipleend'])) {
$select[] = $this->Html->tags['checkboxmultipleend'];
}
} else {
$select[] = $this->Html->tags['selectend'];
}
return $this->output(implode("\n", $select));
}
/**
@ -1390,7 +1411,7 @@ class FormHelper extends AppHelper {
function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) {
$select = array();
$attributes = am(array('escape' => true), $attributes);
$attributes = am(array('escape' => true, 'style' => null), $attributes);
$selectedIsEmpty = ($selected === '' || $selected === null);
$selectedIsArray = is_array($selected);
@ -1414,14 +1435,29 @@ class FormHelper extends AppHelper {
}
if ($name !== null) {
if ((!$selectedIsEmpty && ($selected == $name)) || ($selectedIsArray && in_array($name, $selected))) {
if ($attributes['style'] === 'checkbox') {
$htmlOptions['checked'] = true;
} else {
$htmlOptions['selected'] = 'selected';
}
}
if ($showParents || (!in_array($title, $parents))) {
$title = ife($attributes['escape'], h($title), $title);
if ($attributes['style'] === 'checkbox') {
$htmlOptions['value'] = $name;
$label = array();
if (isset($htmlOptions['checked']) && $htmlOptions['checked'] === true) {
$label['class'] = 'selected';
}
list($name) = array_values($this->__name());
$select[] = sprintf($this->Html->tags['checkboxmultiple'], $name, $this->Html->_parseAttributes($htmlOptions)) . $this->label(null, $title, $label);
} else {
$select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->_parseAttributes($htmlOptions), $title);
}
}
}
}
return array_reverse($select, true);
}
@ -1467,7 +1503,7 @@ class FormHelper extends AppHelper {
break;
case 'month':
for ($i = 1; $i <= 12; $i++) {
$data[sprintf("%02s", $i)] = strftime("%B", mktime(1,1,1,$i,1,1999));
$data[sprintf("%02s", $i)] = strftime("%B", mktime(1, 1, 1, $i, 1, 1999));
}
break;
case 'year':

View file

@ -55,6 +55,7 @@ class HtmlHelper extends AppHelper {
'hidden' => '<input type="hidden" name="%s" %s/>',
'textarea' => '<textarea name="%s" %s>%s</textarea>',
'checkbox' => '<input type="checkbox" name="%s" %s/>',
'checkboxmultiple' => '<input type="checkbox" name="%s[]"%s />',
'radio' => '<input type="radio" name="%s" id="%s" %s />%s',
'selectstart' => '<select name="%s"%s>',
'selectmultiplestart' => '<select name="%s[]"%s>',