Can't set selected value for Zend_Form dropdown element

960 Views Asked by At

While editing all fields in a form are populated correctly except dropdown menu and I can't figure out what's wrong.

Here's my form

        $katModel = new Model_Kategoria();
        $kategorie = $katModel->getKategorie(0);

        $katList = new External_Form_Element_SelectAttribs('kategoria');
        $katList->setLabel('Kategoria: ');
        $katList->isRequired(true);
        if(isset($kategorie)) {
            foreach($kategorie as $k) {
                $katList->addOption($k['id'], $k['kategoria'], array('class' => 'level-' . $k['depth']));
            }
        }
        $this->addElement($katList);

I'm using custom element so I can add class to "option"

class External_Form_Element_SelectAttribs extends Zend_Form_Element {

public $options = array();

public $helper = 'selectAttribs';

/**
 * Adds a new <option>
 * @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->options[$value] = array(
        'value' => $value,
        'label' => $label
    ) + $attribs;
    return $this;
}
}

In my controller's edit action I have

$zadanie = $zadanieModel->find($id)->current();
$zadanieForm->populate($zadanie->toArray());

Which populates all fields corretly except that one

I've tried

$zadanieForm->kategoria->setValue(7);
$zadanieForm->setDefault('kategoria', 7);   

But it didn't work.

Now I'm guessing it something with that custom element I'm using (found the code on google) but I don't know how to modify it so it'll work for me.

1

There are 1 best solutions below

0
On

I use following code to create drop-down with numbers from 1 to 10. Then I am selecting 9 as default :

$var = new Zend_Form_Element_Select('ElementName');

    for ($i = 1; $i <=10; $i++) {
        $priority->addMultiOption($i,$i);
    }

    $priority->setValue(9);