Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZEND, Edit form

I have a Zend form to add something to database. And then I want to use this form to edit what I added to the databese. Is any possibility to use this form (fill it from database and display it???) I have this in my controller:

public function editAction() {

    if (Zend_Auth::getInstance()->hasIdentity()) {
        try {
            $form = new Application_Form_NewStory();
            $request = $this->getRequest();
            $story = new Application_Model_DbTable_Story();
            $result = $story->find($request->getParam('id'));

           // $values = array(
           //     'title' => $result->title,
           //     'story' => $result->story,
           // );

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $data = array(
                        'title' => $form->getValue("title"),
                        'story' => $form->getValue("story"),
                    );
                    $where = array(
                        'id' => $request->getParam('id'),
                    );
                    $story->update($data, $where);
                }
            }
            $this->view->form = $form;
            $this->view->titleS= $result->title;
            $this->view->storyS= $result->story;
        } catch (Exception $e) {
            echo $e;
        }
    } else {
        $this->_helper->redirector->goToRoute(array(
            'controller' => 'auth',
            'action' => 'index'
        ));
    }
}

In my view:

<?php
try
{
$tmp = $this->form->setAction($this->url());
//$tmp->titleS=$this->title;
//$tmp->storyS=$this->story;

//echo $tmp->title = "aaaaa";
}
catch(Exception $e)
{
    echo $e;
}

And when I try to change something in this view I mean give any value different then NULL I have error that I can not do it so is any possibility to reuse this form? Or not?

Thanks!

like image 306
canimbenim Avatar asked Jan 29 '11 22:01

canimbenim


1 Answers

Zend_Form has method populate(), which sets values of the form based on array data. So just do:

$form->populate($result->current()->toArray());

and form will be populated based on keys from array.

like image 134
Radek Benkel Avatar answered Nov 24 '22 11:11

Radek Benkel