Browse Source

use actual value in selected/disabled check

IWASE 11 years ago
parent
commit
b90cf78401

+ 1 - 0
src/View/Widget/SelectBoxWidget.php

@@ -241,6 +241,7 @@ class SelectBoxWidget implements WidgetInterface
             ];
             if (is_array($val) && isset($optAttrs['text'], $optAttrs['value'])) {
                 $optAttrs = $val;
+                $key = $optAttrs['value'];
             }
             if ($this->_isSelected($key, $selected)) {
                 $optAttrs['selected'] = true;

+ 62 - 0
tests/TestCase/View/Widget/SelectBoxWidgetTest.php

@@ -211,6 +211,37 @@ class SelectBoxWidgetTest extends TestCase
     }
 
     /**
+     * test complex option rendering with a selected value
+     *
+     * @return void
+     */
+    public function testRenderComplexSelected()
+    {
+        $select = new SelectBoxWidget($this->templates);
+        $data = [
+            'id' => 'BirdName',
+            'name' => 'Birds[name]',
+            'val' => 'a',
+            'options' => [
+                ['value' => 'a', 'text' => 'Albatross'],
+                ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
+            ]
+        ];
+        $result = $select->render($data, $this->context);
+        $expected = [
+            'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
+            ['option' => ['value' => 'a', 'selected' => 'selected']],
+            'Albatross',
+            '/option',
+            ['option' => ['value' => 'b', 'data-foo' => 'bar']],
+            'Budgie',
+            '/option',
+            '/select'
+        ];
+        $this->assertHtml($expected, $result);
+    }
+
+    /**
      * test rendering a multi select
      *
      * @return void
@@ -575,6 +606,37 @@ class SelectBoxWidgetTest extends TestCase
     }
 
     /**
+     * test complex option rendering with a disabled element
+     *
+     * @return void
+     */
+    public function testRenderComplexDisabled()
+    {
+        $select = new SelectBoxWidget($this->templates);
+        $data = [
+            'disabled' => ['b'],
+            'id' => 'BirdName',
+            'name' => 'Birds[name]',
+            'options' => [
+                ['value' => 'a', 'text' => 'Albatross'],
+                ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
+            ]
+        ];
+        $result = $select->render($data, $this->context);
+        $expected = [
+            'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
+            ['option' => ['value' => 'a']],
+            'Albatross',
+            '/option',
+            ['option' => ['value' => 'b', 'data-foo' => 'bar', 'disabled' => 'disabled']],
+            'Budgie',
+            '/option',
+            '/select'
+        ];
+        $this->assertHtml($expected, $result);
+    }
+
+    /**
      * test rendering with an empty value
      *
      * @return void