1

In my form, I want to set the selected (default) value for a select element. However, using setDefaults is not working for me.

Here is my code:

$gender = new Zend_Form_Element_Select('sltGender');
$gender->setMultiOptions(array(
    -1 => 'Gender',
    0 => 'Female',
    1 => 'Male'
))
->addValidator(new Zend_Validate_Int(), false)
->addValidator(new Zend_Validate_GreaterThan(-1), false);

$this->setDefaults(array(
    'sltGender' => 0
));

$this->addElement($gender);

My controller is simply assigning the form to a view variable which just displays the form.

It works by using $gender->setValue(0), but it would be easier to set them all at once with an array of default values. Am I misunderstanding something here?

Also, where is the Zend Framework documentation for classes and methods? I am looking for something similar to the Java documentation. The best I could find is this one, but I don't like it - especially because every time I try to search, it crashes.

1 Answer 1

3

Have you tried:

$this->addElement($gender);

$this->setDefaults(array(
    'sltGender' => 0
));

Also, take a look at http://framework.zend.com/issues/browse/ZF-12021 .

As you can see, the above issue is similar to the issue you're describing. It seems Zend is very particular about the order you create objects and assign settings.

I'm afraid you're going to have to do things in the order Zend wants you to do them (which doesn't seem well documented, but is only discovered thru trial and error), or hack their library to make it do what you want it to do.

Sign up to request clarification or add additional context in comments.

3 Comments

That worked. Thank you so much! I would never have figured that out, both because I am on my second day with ZF and because I do not find it logical. Other options you specify before adding the element, but apparently not defaults. I find it very strange and not logical. Thanks again, you really saved me hours of headache on this one! :-)
I agree. It doesn't seem logical to me either! See framework.zend.com/issues/browse/…
Well... I think this is very logical. If you are calling $this->setDefaults() it means you are calling The Form itself and you tell it to set deafult value for element named sltGender but how is this possible, if The Form owns no such element YET ? First of all you must add this element to The Form. I can imagine that surprised form saying "Ain't got no sltGender man!" :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.