簡體   English   中英

如何使用每個字段旁邊的編輯按鈕編輯Zend表單

[英]How to edit Zend form with edit button next to each field

我有文件(ProfileController.php),其中包含以下代碼:

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(
           //     'names' => $result->names,
           //     'password' => $result->password,
           // );

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $data = array(
                        'names' => $form->getValue("names"),
                        'password' => $form->getValue("password"),
                    );
                    $form->populate($data->toArray());
                    $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'
        ));
    }

    }

以及另一個包含以下代碼的文件(edit.phtml):

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

    ?>

我希望用戶能夠編輯其用戶名和密碼。 我該怎么辦?

首先:將Zend_Auth內容上移到init()或preDispatch()上,這樣Auth將針對控制器中的任何或所有操作運行。

使一個提交按鈕起作用的訣竅是為按鈕賦予不同的名稱,以便getParam('')可以使用。

通常,我僅在執行刪除操作時才執行此類操作,對於編輯或更新操作,我只是將整個數組提交回數據庫。 我通常使用Zend_Db_Table_Row save()方法代替Zend_Db_Table's insert()update()因此機制有所不同。

我只是使用一個簡單的表單來執行更新,這是控制器代碼(視圖只是回顯表單):

//update album information
public function updatealbumAction()
    {   //get page number from session
        $session = new Zend_Session_Namespace('page');
        //get album id
        $id = $this->getRequest()->getParam('id');

        $model = new Music_Model_Mapper_Album();
        //fetch the album database record
        $album = $model->findById($id);

        $form = new Admin_Form_Album();
        //this form is used elsewhere so set the form action to this action
        $form->setAction('/admin/music/updatealbum/');

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $data = $form->getValues();//get valid and filtered form values

                $newAlbum = new Music_Model_Album($data);//create new entity object

                $update = $model->saveAlbum($newAlbum);//save/update album info

                $this->message->addMessage("Update of Album '$update->name' complete!");//generate flash message
                $this->getHelper('Redirector')->gotoSimple('update', null, null, array('page' => $session->page));//redirect back to the page the request came from
            }
        } else {
            $form->populate($album->toArray());
            $this->view->form = $form;
        }
    }

這是一個非常常見的更新操作。

現在,這是您可能如何使用不同的請求參數對記錄執行操作的方式。 我用它來刪除數據庫記錄,但是一切皆有可能。

public function deleteAction()
    {
        $session = new Zend_Session_Namespace('page');
        $request = $this->getRequest()->getParams();
        try {
            switch ($request) {
                //if 
                case isset($request['trackId']):
                    $id = $request['trackId'];
                    $model = new Music_Model_Mapper_Track();
                    $model->deleteTrack($id);
                    $this->message->addMessage("Track Deleted!");

                    break;
                case isset($request['albumId']):
                    $id = $request['albumId'];
                    $model = new Music_Model_Mapper_Album();
                    $model->deletealbum($id);
                    $this->message->addMessage("Album Deleted!");

                    break;
                case isset($request['artistId']):
                    $id = $request['artistId'];
                    $model = new Music_Model_Mapper_Artist();
                    $model->deleteArtist($id);
                    $this->message->addMessage("Artist Deleted!");

                    break;

                default:
                    break;
            }
            $this->getHelper('Redirector')->gotoSimple('update', null, null, array('page' => $session->page));
        } catch (Exception $e) {
            $this->message->addMessage($e->getMessage());
            $this->getHelper('Redirector')->gotoSimple('update', null, null, array('page' => $session->page));
        }
    }

您可以將請求參數傳遞為“提交”按鈕標簽或url或任何適合您的參數。

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM