[Solar-svn] Revision 3149

pmjones at solarphp.com pmjones at solarphp.com
Sat May 3 16:46:30 CDT 2008


Solar_View_Helper_FormSelect: [ADD] Now supports <optgroup> collections when the option label is an array.  Thanks for the patch, Rodrigo Moraes.

fixes #19


Modified: trunk/Solar/View/Helper/FormSelect.php
===================================================================
--- trunk/Solar/View/Helper/FormSelect.php	2008-05-03 21:37:16 UTC (rev 3148)
+++ trunk/Solar/View/Helper/FormSelect.php	2008-05-03 21:46:30 UTC (rev 3149)
@@ -59,16 +59,23 @@
         
         // build the list of options
         $list = array();
-        foreach ($this->_options as $opt_value => $opt_label) {
-            $selected = '';
-            if (in_array($opt_value, $this->_value)) {
-                $selected = ' selected="selected"';
+        foreach ($this->_options as $value => $label) {
+            if (is_array($label)) {
+                
+                // Use <optgroup>
+                $list[] = '<optgroup label="'
+                        . $this->_view->escape($value)
+                        . '">';
+                
+                foreach ($label as $grp_value => $grp_label) {
+                    $list[] = $this->_getOption($grp_value, $grp_label);
+                }
+                
+                $list[] = '</optgroup>';
+                
+            } else {
+                $list[] = $this->_getOption($value, $label);
             }
-            $list[] = '<option'
-                    . ' value="' . $this->_view->escape($opt_value) . '"'
-                    . ' label="' . $this->_view->escape($opt_label) . '"'
-                    . $selected
-                    . '>' . $this->_view->escape($opt_label) . "</option>";
         }
         
         // build and return the remaining xhtml
@@ -78,4 +85,32 @@
              . "    " . implode("\n    ", $list) . "\n"
              . "</select>";
     }
+    
+    /**
+     *
+     * Builds an option for the select.
+     *
+     * @param string $value The option value.
+     *
+     * @param string $label The option lavel.
+     * 
+     * @return string The option XHTML.
+     * 
+     */
+    protected function _getOption($value, $label)
+    {
+        $selected = '';
+        
+        if (in_array($value, $this->_value)) {
+            $selected = ' selected="selected"';
+        }
+        
+        $option = '<option'
+                . ' value="' . $this->_view->escape($value) . '"'
+                . ' label="' . $this->_view->escape($label) . '"'
+                . $selected
+                . '>' . $this->_view->escape($label) . "</option>";
+        
+        return $option;        
+    }
 }




More information about the Solar-svn mailing list