Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is not possible using ZF's implementation of <code>Zend_Form_Element_Select</code>. You need to create your own element. I have done something similar, here's the relevant code:</p> <pre><code>&lt;?php require_once 'Zend/Form/Element/Select.php'; /** * Select, but with the possibility to add attributes to &lt;option&gt;s * @author Dominik Marczuk */ class Zend_Form_Element_SelectAttribs extends Zend_Form_Element { public $options = array(); public $helper = 'selectAttribs'; /** * Adds a new &lt;option&gt; * @param string $value value (key) used internally * @param string $label label that is shown to the user * @param array $attribs additional attributes */ public function addOption ($value,$label = '',$attribs = array()) { $value = (string) $value; if (!empty($label)) $label = (string) $label; else $label = $value; $this-&gt;options[$value] = array( 'value' =&gt; $value, 'label' =&gt; $label ) + $attribs; return $this; } } </code></pre> <p>Put this into /library/Zend/Form/Element/SelectAttribs.php. You also need a helper to render the element. Put it into your view helpers directory, name it SelectAttribs.php as well. Here's the contents of my file:</p> <pre><code>&lt;?php require_once 'Zend/View/Helper/FormElement.php'; class Zend_View_Helper_SelectAttribs extends Zend_View_Helper_FormElement { public function selectAttribs($name, $value = null, $attribs = null, $options = null, $listsep = "&lt;br /&gt;\n") { $info = $this-&gt;_getInfo($name, $value, $attribs, $options, $listsep); extract($info); // name, id, value, attribs, options, listsep, disable // force $value to array so we can compare multiple values to multiple // options; also ensure it's a string for comparison purposes. $value = array_map('strval', (array) $value); // now start building the XHTML. $disabled = ''; if (true === $disable) { $disabled = ' disabled="disabled"'; } // Build the surrounding select element first. $xhtml = '&lt;select' . ' name="' . $this-&gt;view-&gt;escape($name) . '"' . ' id="' . $this-&gt;view-&gt;escape($id) . '"' . $disabled . $this-&gt;_htmlAttribs($attribs) . "&gt;\n "; // build the list of options $list = array(); $translator = $this-&gt;getTranslator(); foreach ($options as $opt_value =&gt; $option) { $opt_disable = ''; if (is_array($disable) &amp;&amp; in_array($opt_value, $disable)) { $opt_disable = ' disabled="disabled"'; } $list[] = $this-&gt;_build($option, $disabled); } // add the options to the xhtml and close the select $xhtml .= implode("\n ", $list) . "\n&lt;/select&gt;"; return $xhtml; } protected function _build($option, $disabled) { $html = '&lt;option'; foreach ($option as $attrib =&gt; $value) { $html .= " $attrib=\"$value\""; } return $html.$disabled."&gt;".$option['label']."&lt;/option&gt;"; } } </code></pre> <p>With this, you should be ready to go:</p> <pre><code>$elt = new Zend_Form_Element_SelectAttribs('whatever'); $elt-&gt;addOption($value,$label,array('attribname' =&gt; 'attribvalue')); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload