ChamingaD
ChamingaD

Reputation: 2928

Zend Form Binding with Multiple Tables

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

Answers (1)

ARIF MAHMUD RANA
ARIF MAHMUD RANA

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:

  • The composed Hydrator calls extract() on the object, and uses the values returned, if any, to populate the value attributes of all elements. If a form contains a fieldset that itself contains another fieldset, the form will recursively extract the values.
  • When isValid() is called, if setData() has not been previously set, the form uses the composed Hydrator to extract values from the object, and uses those during validation.
  • If isValid() is successful (and the bindOnValidate flag is enabled, which is true by default), then the Hydrator will be passed the validated values to use to hydrate the bound object. (If you do not want this behavior, call setBindOnValidate(FormInterface::BIND_MANUAL)).
  • If the object implements Zend\InputFilter\InputFilterAwareInterface, the input filter it composes will be used instead of the one composed on the form.

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

Related Questions