0
votes

I'm a Zend Framework newbie and I'm trying to retrieve some values to update the database. I have the following code in the controller. Populating the form works just fine, as does updating the db with some hard-coded values. My problem lies in trying to retrieve an updated value from the form, see the $first_name variable. It gives me a fatal error.

My question: How do I retrieve an updated value from the form?

public function editAction() {

    $view = $this->view;

    //get the application
    $application_id = $this->getRequest()->getParam('id');
    $application = $this->getApplication($application_id);

    //get the applicant
    $applicant_id = $application->applicant_id;
    $applicant = $this->getApplicant($applicant_id);     

    if ($this->getRequest()->isPost()) {
        if ($this->getRequest()->getPost('Save')) {

            $applicants_table = new Applicants();

            $first_name = $form->getValue('applicant_first_name');

            $update_data = array ('first_name' => 'NewFirstName',
                                  'last_name' => 'NewLastName');

            $where = array('applicant_id = ?' => 16);

            $applicants_table->update($update_data, $where);

        }

        $url = 'applications/list';
        $this->_redirect($url);

    } else { //populate the form

        //applicant data
        $applicant_data = array();
        $applicant_array = $applicant->toArray();
        foreach ($applicant_array as $field => $value) {
            $applicant_data['applicant_'.$field] = $value;
        }

        $form = new FormEdit();
        $form->populate($applicant_data);

        $this->view->form = $form;

    }
}
1

1 Answers

0
votes

First off, your example has a problem here:

$first_name = $form->getValue('applicant_first_name');

...your $form isn't created yet, thus the fatal error; you are calling getValue() on a non-object.

Once you get that squared away, you can populate the form with posted data by calling the isValid method on the $form with request data. Here's a quick example:

// setup $application_data

$form = new FormEdit();
$form->populate($applicant_data);

if ($this->getRequest()->isPost()) {
    if ($form->isValid($this->getRequest()->getPost())) {
        $first_name = $form->getValue('applicant_first_name');

        // save data to the database ... 

    }
}