Reputation: 73
How to populate a select element in ZF 2 ? My entity object has correct value. When i bind object with form, form doesn't populate the select element with actual value.
Can't i do with just $oForm->bind($oMyEntityObject);
Note :: I am not talking about Select element's 'value_options' . I have tried to manually call setAttribute() method and set 'value' property. I just want to know is there anything i can do with bind() method ( $oForm->bind() )
Upvotes: 1
Views: 146
Reputation: 9857
bind()
will use the form hydrator that is attached to the form to set the value, so it will depend on which type you use. The class methods hydrator would for instance, would call $oMyEntityObject
's public methods that match the form element names.
It is therefore important that the select element foo
has a corresponding class property foo
and the class has a publicly accessible $oMyEntityObject->getFoo()
.
Select elements will also have the additional requirement of the value needing to be already within the value_options
(So if $oMyEntityObject->getFoo() == 1
then your would require 'value_options' => array(1 => 'Foo option 1')
for it to be correctly set using bind($oMyEntityObject)
).
Upvotes: 1