Edgar Kenji Tanaka
Edgar Kenji Tanaka

Reputation: 73

Zend_Form_Element_Select setValue is selecting more than one option

I am using Zend 1.11.10 and I am trying to set a value in a dropdown list. My code is:

    $state = new Zend_Form_Element_Select("mytest");
    $state->setLabel("mytest")  
    ->setName("mytest");

    $state->addMultiOption('Pear','PE');
    $state->addMultiOption('Banana','BA');
    $state->addMultiOption('Orange','OR');
    $state->addMultiOption('Kiwi','KI');

    $state->setValue('Banana');

    $this->addElement($state);

The problem is that the generated HTML code is:

<select id="mytest" name="mytest" style="opacity: 0;"><option value="PE">Pear</option><option selected="selected" value="BA">Banana</option><option selected="selected" value="OR">Orange</option><option selected="selected" value="KI">Kiwi</option></select>

It is making "selected" all options after "Banana". Is this a bug in Zend?

Upvotes: 0

Views: 4993

Answers (2)

Phil
Phil

Reputation: 164760

Looks to me like you're using a non-standard FormSelect view helper. For starters, your <option> value attribute and text values are reversed and you have no label attributes. Eg, for

$state->addMultiOption('Pear','PE');

the generated markup should be

<option value="Pear" label="PE">PE</option>

I'd also hazard a guess that there's some JavaScript playing with the DOM due to the opacity style attribute.

Upvotes: 1

Tim Fountain
Tim Fountain

Reputation: 33148

Using your code exactly I get:

<select name="mytest" id="mytest"> 
    <option value="Pear" label="PE">PE</option> 
    <option value="Banana" label="BA" selected="selected">BA</option> 
    <option value="Orange" label="OR">OR</option> 
    <option value="Kiwi" label="KI">KI</option> 
</select>

this is also with 1.11.10. Are you using custom form classes or anything else that might be affecting it?

Upvotes: 1

Related Questions