Ticket #19 (closed enhancement: fixed)

Opened 2 years ago

Last modified 2 months ago

FormSelect helper: allow optgroups

Reported by: pmjones Assigned to: pmjones
Priority: minor Component: code
Keywords: Cc:

Description

Per note from Rodrigo:

You know, the FormSelect helper doesn't allow optgroups. I've hacked
it to allow groups. When an option label is an array, it uses the
option value as the group label and the label as the options array. It
is dirty, but it works. :-)

if(is_array($opt_label)) {
    $list[] = '<optgroup label="' . $opt_value . '">';
    foreach ($opt_label as $opt_group_value => $opt_group_label) {
        $selected = '';
        if (in_array($opt_group_label, $this->_value)) {
            $selected = ' selected="selected"';
        }
        $list[] = '<option'
            . ' value="' . $this->_view->escape($opt_group_value) . '"'
            . ' label="' . $this->_view->escape($opt_group_label) . '"'
            . $selected
            . '>' . $this->_view->escape($opt_group_label) . "</option>";
    }
    $list[] = '</optgroup>';
} else {
... current FormSelect options building ...

Attachments

Change History

10/16/06 09:05:45 changed by pmjones

Note from zf-general relating to optgroups:

I needed a form select that could use optgroups... I patched the helper FormSelect, and figured i'd throw it here to see if it could get included.  Do I need to make a tracker issue?

Index: /usr/local/share/zend/library/zend/Zend/View/Helper/FormSelect.php
===================================================================
--- /usr/local/share/zend/library/zend/Zend/View/Helper/FormSelect.php	(revision 721)
+++ /usr/local/share/zend/library/zend/Zend/View/Helper/FormSelect.php	(working copy)
@@ -114,20 +114,21 @@
             // build the list of options
             $list = array();
             foreach ($options as $opt_value => $opt_label) {
-
-                // option value and label
-                $opt = '<option'
-                     . ' value="' . htmlspecialchars($opt_value) . '"'
-                     . ' label="' . htmlspecialchars($opt_label) . '"';
-
-                // selected?
-                if (in_array($opt_value, $value)) {
-                    $opt .= ' selected="selected"';
-                }

-                // close and add
-                $opt .= '>' . htmlspecialchars($opt_label) . "</option>";
-                $list[] = $opt;
+                // if we have multiple items
+                // put them into a group
+                if (is_array($opt_label)) {
+                    $list[] = '<optgroup'
+                            . ' label="' . htmlspecialchars($opt_value) . '">';
+
+                    foreach($opt_label as $opt_value => $opt_label) {
+                        $list[] = $this->_build($value, $opt_value, $opt_label);
+                    }
+
+                    $list[] = '</optgroup>';
+                } else {
+                    $list[] = $this->_build($value, $opt_value, $opt_label);
+                }
             }

             // add the options to the xhtml and close the select
@@ -137,4 +138,35 @@

         return $xhtml;
     }
+
+    /**
+     *  Builds the actual <option> tag
+     *
+     * @access private
+     *
+     *
+     * @param mixed $value The option value to mark as 'selected'
+     *
+     * @param string $opt_value Options Value
+     *
+     * @param string $opt_label Options Label
+     *
+     * @return string Option Tag XHTML
+     */
+    private function _build($selected, $opt_value, $opt_label)
+    {
+        $opt = '<option'
+             . ' value="' . htmlspecialchars($opt_value) . '"'
+             . ' label="' . htmlspecialchars($opt_label) . '"';
+
+        // selected?
+        if (in_array($opt_value, $selected)) {
+            $opt .= ' selected="selected"';
+        }
+
+        // close and add
+        $opt .= '>' . htmlspecialchars($opt_label) . "</option>";
+
+        return $opt;
+    }
}

10/16/06 09:16:51 changed by moraes <rodrigo.moraes@gmail.com>

The zf-general proposal is similar to mine, with an improvement. It also checks if the option label is an array to build an optgroup, but adds a _build() method to avoid repeated code for the common options.

01/18/08 05:04:29 changed by moraes

Here's a patch to allow optgroups in Solar_View_Helper_FormSelect:

Index: /home/moraes/workspace/Solar-SVN/Solar/View/Helper/FormSelect.php
===================================================================
--- /home/moraes/workspace/Solar-SVN/Solar/View/Helper/FormSelect.php	(revision 2976)
+++ /home/moraes/workspace/Solar-SVN/Solar/View/Helper/FormSelect.php	(working copy)
@@ -49,15 +49,16 @@
         // 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"';
+            if(is_array($opt_label)) {
+                // Use <optgroup>
+                $list[] = '<optgroup label="' . $opt_value . '">';
+                foreach ($opt_label as $optgroup_value => $optgroup_label) {
+                    $list[] = $this->_getOption($optgroup_value, $optgroup_label);
+                }
+                $list[] = '</optgroup>';
+            } else {
+                $list[] = $this->_getOption($opt_value, $opt_label);
             }
-            $list[] = '<option'
-                    . ' value="' . $this->_view->escape($opt_value) . '"'
-                    . ' label="' . $this->_view->escape($opt_label) . '"'
-                    . $selected
-                    . '>' . $this->_view->escape($opt_label) . "</option>";
         }
         
         // now build the XHTML
@@ -66,4 +67,30 @@
              . "    " . implode("\n    ", $list) . "\n"
              . "</select>";
     }
+    
+    /**
+     *
+     * Builds an option for the select.
+     *
+     * @param string $value The option value.
+     *
+     * @param string $label The option lavel.
+     *
+     */
+    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;        
+    }
 }

05/03/08 16:47:21 changed by pmjones

  • status changed from new to closed.
  • resolution set to fixed.

Patch applied in [3149] -- thanks, Rodrigo. :-)


Add/Change #19 (FormSelect helper: allow optgroups)




Action