ZEND,编辑表单

5

我有一个Zend表单用于向数据库添加内容。然后,我希望使用此表单编辑我添加到数据库中的内容。有没有可能使用此表单(从数据库填充并显示它)?

我在控制器中有以下代码:

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'
        ));
    }
}

在我看来:

<?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;
}

当我尝试在这个视图中改变任何值并给出除了NULL之外的值时,会出现错误提示,我无法这样做,那么是否有可能重用这个表单呢?还是不行?

谢谢!

1个回答

11

Zend_Form 有一个名为 populate() 的方法,可以根据数组数据设置表单的值。因此只需执行以下操作:

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

表单将根据数组中的键填充。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接