【问题标题】:ZEND, Edit formZEND,编辑表格
【发布时间】:2011-01-29 22:45:56
【问题描述】:

我有一个 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 不同的值我有错误,我不能这样做,所以是否有可能重用此表单?还是不行?

谢谢!

【问题讨论】:

    标签: zend-framework zend-form


    【解决方案1】:

    Zend_Form 有方法populate(),它根据数组数据设置表单的值。所以就这样做吧:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 2013-03-10
      • 2012-01-09
      • 1970-01-01
      相关资源
      最近更新 更多