Zend Framework addMultiOption adding custom parameters like "rel" for options

4.2k Views Asked by At

Im using Zend, and we have a form thats put together through a series of functions. One in particular addMultiOption, of which is being pulled in from a DB (from where I dunno, cause if I could alter the query, that'd be much easier and better all around. Anyway that said I need to add a "rel" attribute to every option /option so I can preform a task on the option via jquery when that option is selected, its not all options but some, options that don't have extra functionality would have a rel of 0 where as the ones with actions will have a rel of "one" this comes from the DB so. I am trying to figure out how I can add extra attributes outside of value

does the Zend Framework support this, if not how can I achieve this? I found what I thought was a nice post here earlier about it, but turns out it just doesn't work for me

1

There are 1 best solutions below

3
On

Using addMultiOption($value,$label) I just set the value parameter to something like:

$value = $id . '" ref="' . $ref;

and when it renders you get:

<option value="<idValue>" ref="<refValue"><labelValue></option>

Hope this helps....

Okay, value gets escaped but optionClasses does not so inside the loop that adds the addMultiOptions(val,lable) I do something like this:

$optionClasses[<val>] = 'ref_' . <val> . '" ref="' . <ref>;

and then after the loop just do a setAttrib('optionClasses',$optionClasses)

And that actually works...

So here is an example where I will define an array that might be a recordset from a db query with the three parts we are going to use; id, code and offset

$records = array( array('id' => 1, 'code' => 'Code 1', 'offset' => 4),
                  array('id' => 2, 'code' => 'Code 2', 'offset' => 5),
                  array('id' => 3, 'code' => 'Code 3', 'offset' => 6)
           );

and I will use this in a form element definition ( in a class that extends Zend_Form) for a select where the options have an attribute called 'offset' that will have the 'offset' value from the array

$e = $this->createElement('select', 'code_id');
$e->setLabel('Event Type:')
    ->setAttrib('size', 1);
$optionClasses = array();
foreach ($records as $record) {
    $optionClass = 'xcode_' . $record['id'] . '" xoffset="' . $record['offset'];
    $optionClasses[$record['id']] = $optionClass;
    $e->addMultiOption($record['id'],$record['code']);
}
$e->setAttrib('optionClasses', $optionClasses);
$this->addElement($e);

and when this is rendered, it produces a select element like

<select name="code_id" id="code_id">
    <option value="1" class="xcode_1" xoffset="4" selected="selected">Code 1</option>
    <option value="2" class="xcode_2" xoffset="5">Code 2</option>
    <option value="3" class="xcode_3" xoffset="6">Code 3</option>
</select>

and then you can get to the offset of the selected option with jQuery using

 var xoffset = $("#code_id").find("option:selected").attr("xoffset");

this works because the optionClasses resulting class attribute does not get escaped by the rendering method like the value attribute does.