Skip to content
Permalink
Browse files

Add optgroup rendering.

  • Loading branch information...
markstory committed Jan 8, 2014
1 parent b0e4d7c commit ff25495c2cbf2c496eab97c5394a2835e08d8391
Showing with 100 additions and 18 deletions.
  1. +28 −15 Cake/View/Input/SelectBox.php
  2. +72 −3 Test/TestCase/View/Input/SelectBoxTest.php
@@ -56,7 +56,7 @@ public function render($data) {
if (empty($data['name'])) {
throw new \RuntimeException('Cannot make inputs with empty name attributes.');
}
$options = $this->_renderOptions($data);
$options = $this->_renderContent($data);
$name = $data['name'];
unset($data['name'], $data['options'], $data['empty'], $data['value']);
if (isset($data['disabled']) && is_array($data['disabled'])) {
@@ -71,7 +71,7 @@ public function render($data) {
]);
}
protected function _renderOptions($data) {
protected function _renderContent($data) {
$out = [];
if (!isset($data['options'])) {
$data['options'] = [];
@@ -92,22 +92,33 @@ protected function _renderOptions($data) {
if (isset($data['disabled']) && is_array($data['disabled'])) {
$disabled = $data['disabled'];
}
return $this->_renderOptions($options, $disabled, $selected);
}
protected function _renderOptions($options, $disabled, $selected) {
foreach ($options as $key => $val) {
$template = 'option';
$isSelected = $this->_isSelected($key, $selected);
$isDisabled = $this->_isDisabled($key, $disabled);
if ($isSelected) {
$template .= 'Selected';
}
if ($isDisabled) {
$template .= 'Disabled';
if (is_array($val)) {
$groupOptions = $this->_renderOptions($val, $disabled, $selected);
$out[] = $this->_templates->format('optgroup', [
'label' => $key,
'content' => implode('', $groupOptions)
]);
} else {
$template = 'option';
$isSelected = $this->_isSelected($key, $selected);
$isDisabled = $this->_isDisabled($key, $disabled);
if ($isSelected) {
$template .= 'Selected';
}
if ($isDisabled) {
$template .= 'Disabled';
}
$out[] = $this->_templates->format($template, [
'name' => $key,
'value' => $val
]);
}
$out[] = $this->_templates->format($template, [
'name' => $key,
'value' => $val
]);
}
return $out;
}
@@ -157,6 +168,8 @@ protected function _parseAttributes($options, $exclude = null) {
* Formats an individual attribute, and returns the string value of the composed attribute.
* Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked'
*
* TODO MOVE TO StringTemplate class?
*
* @param string $key The name of the attribute to create
* @param string $value The value of the attribute to create.
* @param boolean $escape Define if the value must be escaped
@@ -189,16 +189,85 @@ public function testRenderMultipleSelected() {
* @return void
*/
public function testRenderOptionGroups() {
$this->markTestIncomplete('Not done');
$select = new SelectBox($this->templates);
$data = [
'name' => 'Birds[name]',
'options' => [
'Mammal' => [
'beaver' => 'Beaver',
'elk' => 'Elk',
],
'Bird' => [
'budgie' => 'Budgie',
'eagle' => 'Eagle',
]
]
];
$result = $select->render($data);
$expected = [
'select' => [
'name' => 'Birds[name]',
],
['optgroup' => ['label' => 'Mammal']],
['option' => ['value' => 'beaver']],
'Beaver',
'/option',
['option' => ['value' => 'elk']],
'Elk',
'/option',
'/optgroup',
['optgroup' => ['label' => 'Bird']],
['option' => ['value' => 'budgie']],
'Budgie',
'/option',
['option' => ['value' => 'eagle']],
'Eagle',
'/option',
'/optgroup',
'/select'
];
$this->assertTags($result, $expected);
}
/**
* test rendering option groups and selected values
*
* @return void
*/
public function testRenderOptionGroupsSelected() {
$this->markTestIncomplete('Not done');
public function testRenderOptionGroupsSelectedAndDisabled() {
$select = new SelectBox($this->templates);
$data = [
'name' => 'Birds[name]',
'value' => ['1', '2', 'burp'],
'disabled' => ['1x', '2x', 'nope'],
'options' => [
'ones' => [
1 => 'one',
'1x' => 'one x',
],
'twos' => [
'2' => 'two',
'2x' => 'two x',
]
]
];
$result = $select->render($data);
$expected = [
'select' => [
'name' => 'Birds[name]',
],
['optgroup' => ['label' => 'ones']],
['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
['option' => ['value' => '1x', 'disabled' => 'disabled']], 'one x', '/option',
'/optgroup',
['optgroup' => ['label' => 'twos']],
['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
['option' => ['value' => '2x', 'disabled' => 'disabled']], 'two x', '/option',
'/optgroup',
'/select'
];
$this->assertTags($result, $expected);
}
/**

0 comments on commit ff25495

Please sign in to comment.
You can’t perform that action at this time.