I need to update forms on some site written not by me. It works on WordPress 4.3.1 with plugin WP-ZFF Zend Framework Full 1.11.12-1 installed so WP can use Zend classes.
There is a form what I need to check. The code:
class Form_Registration extends Zend_Form {
public function init()
{
$this->setMethod(Zend_Form::METHOD_POST);
$this->setEnctype(Zend_Form::ENCTYPE_URLENCODED);
$this->setAction("/");
$this->setAttrib("id", "registration_form");
$this->setDecorators(array('FormElements', 'tag' => 'form'));
$myselect = $this->createElement("select", "myselect");
$myselect->addMultiOption("1","aa");
$myselect->addMultiOption("2","bb");
$myselect->addMultiOption("3","cc");
$myselect->setValue("2");
echo "value:".$myselect->getValue(); // that prints out "2"
$this->addElements(array($myselect));
}
}
and using of the code:
$f = new Form_Registration();
$f->isValid($_POST);
print $f->render(new Zend_View());
My problem is to set default value for this select element.
I've tried:
- $myselect->setValue("2");
- $this->setDefault('myselect','2');
- $this->populate(array('myselect' => '2'));
Also I've tried to replace $this->createElement()
with $this->addElement()
and so to replace it with
$this->addElement('select', 'myselect', array(
'value' => '2',
'multiOptions' => array(
'1' => 'aa',
'2' => 'bb',
'3' => 'cc'
),
)
);
I've disabled all Javascript to be sure that some script clears form elements. Nothing works! That what I get:
<select id="myselect" name="myselect">
<option value="1">aa</option>
<option value="2">bb</option>
<option value="3">cc</option>
</select>
What I do wrong and how can I set one of select's option to be 'selected'?