Let's say I have a form that collects a first name and a last name:
$first_name = new Zend_Form_Element_Text('first_name');
$first_name->setLabel("First Name")
->setRequired(true);
$last_name = new Zend_Form_Element_Text('last_name');
$last_name->setLabel("Last Name")
->setRequired(true);
$form = new Zend_Form();
$form->addElement($first_name)
->addElement($last_name)
If I want to use the "populate($data)" or the "setDefaults($data)" method on the form, how does the array need to be organized? What kind of an array do these functions expect? I haven't been able to find this information in the docs.
Also, I know that I can set the value when creating the element itself, but this is not what I need.
The form->populate() method takes an array where the keys are the names of the form fields.
The Zend_Db_Table_Row object implements a toArray() method which can be used here (as do many other objects). So you can do stuff like:
$form = new MyForm;
$table = new MyTable;
$rowset = $table->find($id);
$row = $rowset->current();
$form->populate($row->toArray());
Array keys are the field names, array values are the field values.
$data = array( 'first_name' => 'Mickey', 'last_name' => 'Mouse' );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With