コード例 #1
0
ファイル: Crud.php プロジェクト: hausdesign/zf-library
 /**
  * Edit action
  */
 public function editAction()
 {
     // Get the request object
     $request = $this->getRequest();
     $populateWithDefaultValues = true;
     // Check if a form is sent
     if ($request->isPost()) {
         $postData = $request->getPost();
         $postData = $this->_parsePostData($postData);
         if (!isset($postData['form_class']) || isset($postData['form_class']) && $postData['form_class'] == get_class($this->_form)) {
             $populateWithDefaultValues = false;
             //Populate the post data to the form
             $this->_form->populate($postData);
             if ($this->_form->cancel->isChecked()) {
                 // Cancel button is pressed
                 $this->_redirect($this->_redirectUrlCancel);
             } elseif ($this->_form->delete !== null && $this->_form->delete->isChecked()) {
                 // Delete button is pressed
                 $this->_redirect($this->_redirectUrlDelete);
             } elseif ($this->validateForm($postData)) {
                 // Form is sent
                 try {
                     // Parse the form data
                     $item = $this->_getItem('edit');
                     if (array_key_exists('form_class', $item)) {
                         unset($item['form_class']);
                     }
                     if (array_key_exists('form_name', $item)) {
                         unset($item['form_name']);
                     }
                     // Save the item
                     $this->_item->setFromArray($item);
                     $this->_item->save();
                     // Actions after the query
                     $this->_afterQuery('edit');
                     // Set the form description
                     $this->_form->setDescription('The item is succesfully saved');
                     $this->_flashMessenger->addMessage('Saved the item succesfully.');
                     // Redirect to the index
                     $this->_redirect($this->_redirectUrlEdit);
                 } catch (Exception $exception) {
                     $this->_form->addErrorMessage($exception->getMessage());
                 }
             }
         }
     }
     if ($populateWithDefaultValues) {
         // Initialize the form with the item values
         $this->_form->populate($this->_getDefaultFormValues());
     }
     // Set the class "error" to subforms with errors
     if (method_exists($this->_form, 'setErrorClass')) {
         $this->_form->setErrorClass('error');
     }
     // Parse the form to the view
     $this->view->form = $this->_form;
 }