Albert Rannetsperger
Albert Rannetsperger

Reputation: 934

Adding extra elements to options array in Zend_Form_Element_Select

Basically, I need an extra bit of data to be included in a select box which is created with Zend. The current markup is as follows:

<select>
    <option label="Canadian Dollar ($)" value="1">Canadian Dollar ($)</option>
    <option label="Euro (€)" value="2">Euro (€)</option>
    <option label="US Dollar ($)" value="3">US Dollar ($)</option>
</select>

The markup I'm trying to achieve is as follows (notice the additional 'data-curreny-type' attribute):

<select>
    <option label="Canadian Dollar ($)" value="1" data-currency-code="CAD">Canadian Dollar ($)</option>
    <option label="Euro (€)" value="2" data-currency-code="EUR">Euro (€)</option>
    <option label="US Dollar ($)" value="3" data-currency-code="USD">US Dollar ($)</option>
</select>

The form is being created using Zend as follows:

$element = new Zend_Form_Element_Select(self::NAME);

/* -- OMMITED DB CODE --*/

$element->setOptions($options);

$options is an array in the form of: array('1' => 'Value1', '2'=> 'Value2').. etc.

My qestion is, is there any way of adding extra elements into the 'options' element using Zend ->setOptions($opts)? Or anything I might not be aware of.

Please help!

Upvotes: 1

Views: 547

Answers (2)

Tim Fountain
Tim Fountain

Reputation: 33148

The FormSelect view helper which renders the element does not support additional attributes on the <option> tags, so the only way to achieve this would be to override that helper with your own implementation. The helper class is Zend_View_Helper_FormSelect, so if you take a look at the code for that you should be able to see how it works.

Upvotes: 2

user2003356
user2003356

Reputation: 455

Please use setArribute method to add an additional attribute. You can add data-currency-code as an attribute.

Upvotes: 0

Related Questions