dongxie9169 2014-03-26 00:07
浏览 21
已采纳

如何在Zend 2框架中将表单连接到另一个phtml视图

I am new at Zend2:

I have a form, and in the first stage I create a new ViewModel and return it:

return new ViewModel(array('form' => $form, 'messages' => $messages));

In the post stage when the data comes back from the browser, how can I connect this same form to a new View (which has the same elements maybe less, maybe more) or create another form and rassign it the old form's data and relate it to a new view to show?

Any help would be appreciated.

EDIT:

I tried to do the following:

$form->setAttribute('action', $this->url('auth/index/login-post.phtml'));

But still shows the old one.

When I do this:

return $this->redirect()->toRoute('auth/default', array('controller' => 'index', 'action' => 'login-post'));

I get error page: The requested controller was unable to dispatch the request.

When I get the post of the request I need to load another view, I mean how do I specify which view is connected to which form?

  • 写回答

2条回答 默认 最新

  • dongqian5569 2014-03-26 09:31
    关注

    The forms do not themselves have any knowledge of the view. If you wish to change the view after completing the form submission; where this new view provides perhaps a different form, this is something that should be done within the controller.

    A (non-working) example with a few options on how a different view could be returned.

    class FooController extends AbstractActionController
    {
    
        public function getFooForm()
        {
            return $this->getServiceLocator()->get('Form\Foo');
        }
    
        public function getBarForm()
        {
            return $this->getServiceLocator()->get('Form\Bar')
        }
    
    
        public function fooAction()
        {
            $request = $this->getRequest();
            $form    = $this->getFooForm();
    
            if ($request->isPost()) {
                $form->setData($request->getPost());
    
                // Is the posted form vaild
                if ($form->isValid()) {
    
                    // Forms validated data
                    $data = $form->getData(); 
    
                    // Now there are a few options
    
                    // 1. Return a new view with the data
                    $view = new ViewModel($data);
                    $view->setTemplate('path/to/file');
    
                    return $view;
    
                    // OR Option 2 - Redirect
    
                    return $this->redirect()->toRoute('bar', $someRouteParams);
    
    
                    // Option 3 - Dispatch a new controller action
                    // and then return it's view model/response
    
                    // We can also pass on the posted data so the controller
                    // action that is dispathed will already have our POSTed data in the
                    // request
                    $request->setPost(new \Zend\Stdlib\Parameters($data));
    
                    return $this->forward()->dispatch('App\Controller\Foo', array('action' => 'bar'));
    
                }
            }
            // Render default foo.phtml or use $view->setTemplate('path/to/view')
            // and render the form, which will post back to itself (fooAction)
            return new ViewModel(array('form' => $form));
        }
    
    
        public function barAction()
        {
            $request = $this->getRequest();
            $form    = $this->getBarForm();
    
            if ($request->isPost()) {
                $form->setData($request->getPost());
                // ....      
            }
    
            // Renders the bar.phtml view
            return $this->viewModel(array('form' => $form));
        }
    
    }
    

    From what I understand form your question, you would need to be using option 3 as the new view should populate a second form with it's already validated data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?