Просмотр исходного кода

Forward-port fix for ZF-11302 (r25052)

git-svn-id: http://framework.zend.com/svn/framework/standard/branches/release-1.12@25053 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 13 лет назад
Родитель
Сommit
83eb7d895d
2 измененных файлов с 43 добавлено и 3 удалено
  1. 23 3
      library/Zend/View/Helper/FormSelect.php
  2. 20 0
      tests/Zend/View/Helper/FormSelectTest.php

+ 23 - 3
library/Zend/View/Helper/FormSelect.php

@@ -52,6 +52,9 @@ class Zend_View_Helper_FormSelect extends Zend_View_Helper_FormElement
      * multiple-select elements).
      *
      * @param array|string $attribs Attributes added to the 'select' tag.
+     * the optional 'optionClasses' attribute is used to add a class to
+     * the options within the select (associative array linking the option
+     * value to the desired class)
      *
      * @param array $options An array of key-value pairs where the array
      * key is the radio value, and the array value is the radio text.
@@ -96,6 +99,13 @@ class Zend_View_Helper_FormSelect extends Zend_View_Helper_FormElement
             unset($attribs['multiple']);
         }
 
+        // handle the options classes
+        $optionClasses = array();
+        if (isset($attribs['optionClasses'])) {
+            $optionClasses = $attribs['optionClasses'];
+            unset($attribs['optionClasses']);
+        }
+        
         // now start building the XHTML.
         $disabled = '';
         if (true === $disable) {
@@ -130,11 +140,11 @@ class Zend_View_Helper_FormSelect extends Zend_View_Helper_FormElement
                         . $opt_id
                         . ' label="' . $this->view->escape($opt_value) .'">';
                 foreach ($opt_label as $val => $lab) {
-                    $list[] = $this->_build($val, $lab, $value, $disable);
+                    $list[] = $this->_build($val, $lab, $value, $disable, $optionClasses);
                 }
                 $list[] = '</optgroup>';
             } else {
-                $list[] = $this->_build($opt_value, $opt_label, $value, $disable);
+                $list[] = $this->_build($opt_value, $opt_label, $value, $disable, $optionClasses);
             }
         }
 
@@ -151,18 +161,28 @@ class Zend_View_Helper_FormSelect extends Zend_View_Helper_FormElement
      * @param string $label Options Label
      * @param array  $selected The option value(s) to mark as 'selected'
      * @param array|bool $disable Whether the select is disabled, or individual options are
+     * @param array $optionClasses The classes to associate with each option value
      * @return string Option Tag XHTML
      */
-    protected function _build($value, $label, $selected, $disable)
+    protected function _build($value, $label, $selected, $disable, $optionClasses = array())
     {
         if (is_bool($disable)) {
             $disable = array();
         }
 
+        $class = null;
+        if (array_key_exists($value, $optionClasses)) {
+            $class = $optionClasses[$value];
+        }
+
+
         $opt = '<option'
              . ' value="' . $this->view->escape($value) . '"'
              . ' label="' . $this->view->escape($label) . '"';
 
+             if ($class) {
+             $opt .= ' class="' . $class . '"';
+         }
         // selected?
         if (in_array((string) $value, $selected)) {
             $opt .= ' selected="selected"';

+ 20 - 0
tests/Zend/View/Helper/FormSelectTest.php

@@ -311,6 +311,7 @@ class Zend_View_Helper_FormSelectTest extends PHPUnit_Framework_TestCase
         $this->assertRegexp('/<select[^>]*?(name="baz\[\]")/', $html, $html);
         $this->assertNotRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
     }
+
     /** 
      * @group ZF-8252
      */
@@ -330,6 +331,25 @@ class Zend_View_Helper_FormSelectTest extends PHPUnit_Framework_TestCase
         $this->assertRegexp('/<optgroup[^>]*?id="baz-optgroup-bar"[^>]*?"bar"[^>]*?/', $html, $html);
     }
  
+    public function testCanApplyOptionClasses()
+    {
+        $html = $this->helper->formSelect(array(
+            'name'    => 'baz[]',
+            'options' => array(
+                'foo' => 'Foo',
+                'bar' => 'Bar',
+                'baz' => 'Baz,'
+            ),
+            'attribs' => array(
+               'multiple' => false,
+               'optionClasses' => array('foo' => 'fooClass',
+                                        'bar' => 'barClass',
+                                        'baz' => 'bazClass')
+            ),
+        ));
+        $this->assertRegexp('/.*<option[^>]*?(value="foo")?(class="fooClass").*/', $html, $html);
+        $this->assertRegexp('/.*<option[^>]*?(value="bar")?(class="barClass").*/', $html, $html);
+    }
 }
 
 // Call Zend_View_Helper_FormSelectTest::main() if this source file is executed directly.