diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 91e57e80c..2bd0ed300 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -545,7 +545,7 @@ class FormHelper extends AppHelper { } if (!empty($div)) { - $divOptions = array('class'=>'input'); + $divOptions = array('class' => 'input'); if (is_string($div)) { $divOptions['class'] = $div; } elseif (is_array($div)) { @@ -567,7 +567,7 @@ class FormHelper extends AppHelper { if (in_array($options['type'], array('date', 'datetime'))) { $labelFor = $this->domId(implode('.', array_filter(array($this->model(), $this->field())))); - $labelAttributes = array( 'for' => $labelFor . 'Month' ); + $labelAttributes = array('for' => $labelFor . 'Month'); } if (is_array($label)) { @@ -975,7 +975,7 @@ class FormHelper extends AppHelper { } $select[] = sprintf($tag, $this->model(), $this->field(), $this->_parseAttributes($attributes)); - if ($showEmpty !== null && $showEmpty !== false) { + if ($showEmpty !== null && $showEmpty !== false && !(empty($showEmpty) && (isset($attributes) && array_key_exists('multiple', $attributes)))) { if ($showEmpty === true) { $showEmpty = ''; } @@ -1268,9 +1268,12 @@ class FormHelper extends AppHelper { * @return array */ function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) { - $attributes = am(array('escape' => true), $attributes); $select = array(); + $attributes = am(array('escape' => true), $attributes); + $selectedIsEmpty = ($selected === '' || $selected === null); + $selectedIsArray = is_array($selected); + foreach ($elements as $name => $title) { $htmlOptions = array(); if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) { @@ -1290,12 +1293,9 @@ class FormHelper extends AppHelper { unset($htmlOptions['name'], $htmlOptions['value']); } if ($name !== null) { - if ($selected !== '' && ($selected !== null) && ($selected == $name)) { - $htmlOptions['selected'] = 'selected'; - } elseif (is_array($selected) && in_array($name, $selected)) { + if ((!$selectedIsEmpty && ($selected == $name)) || ($selectedIsArray && in_array($name, $selected))) { $htmlOptions['selected'] = 'selected'; } - if ($showParents || (!in_array($title, $parents))) { $title = ife($attributes['escape'], h($title), $title); $select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->_parseAttributes($htmlOptions), $title); diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 869b0be11..d43bb2deb 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -480,19 +480,46 @@ class FormHelperTest extends CakeTestCase { $result = $this->Form->select('Model.field', array('first' => 'first "html" ', 'second' => 'value'), null, array(), false); $this->assertPattern('/' . - ']*>\s+' . + ']*>\s*' . ']*>first "html" <chars><\/option>\s+'. ']*>value<\/option>\s+'. - '<\/select>'. - '/i', $result); + '<\/select>/i', + $result); $result = $this->Form->select('Model.field', array('first' => 'first "html" ', 'second' => 'value'), null, array('escape' => false), false); $this->assertPattern('/' . - ']*>\s+' . - ']*>first "html" <\/option>\s+'. - ']*>value<\/option>\s+'. + ']*>\s*' . + '\/]+value="first"[^>]*>first "html" <\/option>\s+'. + '\/]+value="second"[^>]*>value<\/option>\s+'. '<\/select>'. - '/i', $result); + '/i', + $result); + } + + function testSelectMultiple() { + $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), null, array('multiple' => true)); + $this->assertPattern('/^]+name="data\[Model\]\[multi_field\]\[\]"[^<>\/]*>/', $result); + $this->assertPattern('/^]+id="ModelMultiField"[^<>\/]*>/', $result); + $this->assertPattern('/^]+multiple="multiple"[^<>\/]*>/', $result); + $this->assertNoPattern('/^]+[^name|id|multiple]=[^<>\/]*>/', $result); + + $this->assertNoPattern('/option value=""/', $result); + $this->assertNoPattern('/selected/', $result); + $this->assertPattern('/]+value="0">first/', $result); + $this->assertPattern('/]+value="1">second/', $result); + $this->assertPattern('/]+value="2">third/', $result); + $this->assertNoPattern('/]+value="[^012]"[^<>\/]*>/', $result); + $this->assertPattern('/<\/select>$/', $result); + + $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), null, array('multiple' => 'multiple')); + $this->assertPattern('/^]+multiple="multiple"[^<>\/]*>/', $result); + $this->assertNoPattern('/^]+[^name|id|multiple]=[^<>\/]*>/', $result); + + $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array(0, 1), array('multiple' => true)); + $this->assertPattern('/]+value="0"[^<>]+selected="selected">first/', $result); + $this->assertPattern('/]+value="1"[^<>]+selected="selected">second/', $result); + $this->assertPattern('/]+value="2">third/', $result); + $this->assertNoPattern('/]+value="[^012]"[^<>\/]*>/', $result); } function testCheckbox() {