Reputation: 2928
In my Zend application i have separate models for each table and data is retrieved using TableGateway
Now i need to implement form to create edit page. I could able to create form of one table/model as mentioned in http://framework.zend.com/manual/2.2/en/user-guide/forms-and-actions.html
Here is my edit action -
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('candidate', array(
'action' => 'index'
));
}
try {
$candidate = $this->getCandidateTable()->getCandidate($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('candidate', array(
'action' => 'index'
));
}
$form = new CandidateForm();
$form->bind($candidate);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($candidate->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getCandidateTable()->saveCandidate($candidate);
return $this->redirect()->toRoute('candidate');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
edit view -
<?php
$title = 'Edit Candidate';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url(
'candidate',
array(
'action' => 'edit',
'id' => $this->id,
)
));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('title'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
This edit action binds a form with one table (CandidateTable). But in my application, that page has data from multiple tables (CandidateSkills, CandidateQualifications etc). When i click submit it should save data in separate tables.
Upvotes: 0
Views: 1072
Reputation: 5166
You can use setFromArray
You fetch the row object and than just setFromArray and save, commit.
To populate form value use populate method see this Populating and Retrieving Values
$form = new My_Form_Edit();
if( !$this->getRequest()->isPost() )// If the form isn't posted than populate the value
{
$form->populate($myArrayValueToPopulate);//$myArrayValueToPopulate this is your array to populate for the form
return;
}
// Than check validation and save data
For zend framework 2 you can use bind to populate data Binding an object
straight from documentation
When you bind() an object to the form, the following happens:
This is easier to understand in practice.
$contact = new ArrayObject;
$contact['subject'] = '[Contact Form] ';
$contact['message'] = 'Type your message here';
$form = new Contact\ContactForm;
$form->bind($contact); // form now has default values for
// 'subject' and 'message'
$data = array(
'name' => 'John Doe',
'email' => '[email protected]',
'subject' => '[Contact Form] \'sup?',
);
$form->setData($data);
if ($form->isValid()) {
// $contact now looks like:
// array(
// 'name' => 'John Doe',
// 'email' => '[email protected]',
// 'subject' => '[Contact Form] \'sup?',
// 'message' => 'Type your message here',
// )
// only as an ArrayObject
}
Upvotes: 1