Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I didn't find @mingos's answer complete and had some issues with implementing setting the value. His answer helped a lot with what needed to be extended and changed however. Once I had that start the rest was pretty easy. I just extended ZF1's Select and overrode where I needed:</p> <pre><code>/** * Select, now with abbility to specify attributes on &lt;Option&gt;, addMultiOption has new syntax * @author Seth Miller */ class MyNamespace_Form_Element_SelectAttribs extends Zend_Form_Element_Select { public $options = array(); public $helper = 'selectAttribs'; /** * Add an option * * @param string $option * @param string $value * @return Zend_Form_Element_Multi */ public function addMultiOption($value, $label = '', $attribs = array()) { $value = (string) $value; if (!empty($label)) { $label = (string) $label; } else { $label = $value; } $this-&gt;_getMultiOptions(); if (!$this-&gt;_translateOption($value, $label)) { $this-&gt;options[$value] = array( 'value' =&gt; $value, 'label' =&gt; $label ) + $attribs; } return $this; } /** * Add many options at once * * @param array $options * @return Zend_Form_Element_Multi */ public function addMultiOptions(array $options) { foreach ($options as $optionKey =&gt; $optionProperties) { if (is_array($optionProperties) &amp;&amp; array_key_exists('key', $optionProperties) &amp;&amp; array_key_exists('value', $optionProperties) ) { if(array_key_exists('key', $optionProperties)) $optionKey = $optionProperties['key']; $this-&gt;addMultiOption($optionKey, $optionProperties['value'], $optionProperties['attribs']); } else { $this-&gt;addMultiOption($optionKey, $optionProperties); } } return $this; } public function isValid($value, $context = null) { if ($this-&gt;registerInArrayValidator()) { if (!$this-&gt;getValidator('InArray')) { $multiOptions = $this-&gt;getMultiOptions(); $options = array(); foreach ($multiOptions as $optionKey =&gt; $optionData) { // optgroup instead of option label if (is_array($optionData['options'])) { $options = array_merge($options, array_keys($optionData['options'])); } else { $options[] = $optionKey; } } $this-&gt;addValidator( 'InArray', true, array($options) ); } } return parent::isValid($value, $context); } } </code></pre> <p>And the view helper:</p> <p> <p>class MyNamespace_View_Helper_SelectAttribs extends Zend_View_Helper_FormElement {</p> <pre><code>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); // check if element may have multiple values $multiple = ''; if (substr($name, -2) == '[]') { // multiple implied by the name $multiple = ' multiple="multiple"'; } if (isset($attribs['multiple'])) { // Attribute set if ($attribs['multiple']) { // True attribute; set multiple attribute $multiple = ' multiple="multiple"'; // Make sure name indicates multiple values are allowed if (!empty($multiple) &amp;&amp; (substr($name, -2) != '[]')) { $name .= '[]'; } } else { // False attribute; ensure attribute not set $multiple = ''; } unset($attribs['multiple']); } // 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).'"' .$multiple .$disabled .$this-&gt;_htmlAttribs($attribs) ."&gt;\n "; // build the list of options $list = array(); $translator = $this-&gt;getTranslator(); foreach ((array) $options as $optionKey =&gt; $optionData) { if (isset($optionData['options'])) { $optDisable = ''; if (is_array($disable) &amp;&amp; in_array($optionData['value'], $disable)) { $optDisable = ' disabled="disabled"'; } if (null !== $translator) { $optValue = $translator-&gt;translate($optionData['value']); } $optId = ' id="'.$this-&gt;view-&gt;escape($id).'-optgroup-' .$this-&gt;view-&gt;escape($optionData['value']).'"'; $list[] = '&lt;optgroup' .$optDisable .$optId .' label="'.$this-&gt;view-&gt;escape($optionData['value']).'"&gt;'; foreach ($optionData['options'] as $optionKey2 =&gt; $optionData2) { $list[] = $this-&gt;_build($optionKey2, $optionData2, $value, $disable); } $list[] = '&lt;/optgroup&gt;'; } else { $list[] = $this-&gt;_build($optionKey, $optionData, $value, $disable); } } // add the options to the xhtml and close the select $xhtml .= implode("\n ", $list)."\n&lt;/select&gt;"; return $xhtml; } /** * Builds the actual &lt;option&gt; tag * * @param string $value Options Value * @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 * @return string Option Tag XHTML */ protected function _build($optionKey, $optionData, $selected, $disable) { if (is_bool($disable)) { $disable = array(); } $opt = '&lt;option'; foreach ($optionData as $attrib =&gt; $attribValue) { $opt .= ' '.$this-&gt;view-&gt;escape($attrib).'="'.$this-&gt;view-&gt;escape($attribValue).'"'; } // selected? if (in_array((string) $optionData['value'], $selected)) { $opt .= ' selected="selected"'; } // disabled? if (in_array($optionData['value'], $disable)) { $opt .= ' disabled="disabled"'; } $opt .= '&gt;' . $this-&gt;view-&gt;escape($optionData['label']) . "&lt;/option&gt;"; return $opt; } </code></pre> <p>}</p> <p>And implementation in a form would be something like:</p> <pre><code>$selectElement = new MyNamespace_Form_Element_SelectAttribs('selectElementName'); $selectElement-&gt;addMultiOption($value, $label, array('data-custom' =&gt; 'custom data embedded in option tag.'); </code></pre> <p>I hope that helps someone. Thanks.</p>
 

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