【问题标题】:On submit the form don't display its POST data提交表单时不显示其 POST 数据
【发布时间】:2012-03-26 10:55:41
【问题描述】:

我对 Zend 框架有一点经验,但我喜欢摆弄它,直到它工作为止。 但是现在我无法解决这个问题。

我有一个表格:

<?php 
class Application_Form_Login extends Zend_Form
{

protected $notEmpty;

public function init()
{   
    // Create NotEmpty validator
    $notEmpty = new Zend_Validate_NotEmpty();

    // Configure validators for username element
    $notEmpty->setMessage('Gelieve dit veld in te vullen');

    $this->setMethod('post');

    // emailAddress
    $this->addElement('text', 'emailAddress', array(
       'filters' => array('StringTrim', 'StringToLower'),
       'required' => true,
       'validators'    => array(
            array('validator' => $notEmpty),
            ),
       'label' => 'Emailadres:'
    ));

    // password
    $this->addElement('password', 'password', array(
       'filters' => array('StringTrim'),
       'required'      => true,
       'validators'    => array(
           array('validator' => $notEmpty),
           ),
       'label' => 'Wachtwoord:'
    ));

    // submit
    $this->addElement('submit', 'submit', array(
        'ignore' => true,
        'label' => 'Inloggen'
    ));
}
}

一个视图:

<?= $this->form ?>

<?= $this->postdata ?>

还有一个 AccountController:

<?php

class AccountController extends Zend_Controller_Action
{

public function init()
{
    echo 'data:'.$this->getRequest()->getPost('emailAddress');
    /* Initialize action controller here */
}

public function indexAction()
{
    $this->view->postdata = var_dump($this->getRequest()->getParams());

    $form = new Application_Form_Login();
    $request = $this->getRequest();

    if ($request->isPost()){
        // THIS POINT IS NEVER REACHED
    if ($form->isValid($request->getPost())){
            if ($this->_isValidLogin($form->getValues())){
                // Succes Redirect to the home page
                $this->_helper->redirector('index', 'home');
            }
            else // Not succes Redirect to account page
            {
                $this->_helper->redirector('index', 'account');
            }
        }

如您所见,我发表了一条评论:// 永远不会达到这一点。 这个控制器中还有更多功能,但这些与我的问题无关。

让我们再解释一下。 非常奇怪的行为是,当我将数据放入字段时, $this->view->postdata = var_dump($this->getRequest()->getParams() 不返回 POST 数据。 但是当我在登录表单字段中添加注释时,我会看到 POST 数据。当然是空的。 像这样:

array
'controller' => string 'account' (length=7)
'action' => string 'index' (length=5)
'module' => string 'default' (length=7)
'emailAddress' => string '' (length=0)
'password' => string '' (length=0)
'submit' => string 'Inloggen' (length=8)

因此,当没有在登录表单字段中输入任何数据时,实际上不会到达 //THIS POINT IS NEVER REACHED :-)

问题是,我做错了什么?我是否以错误的方式处理 Zend_Controller_Request_Http?

如果您需要更多信息,我应该提供。

【问题讨论】:

    标签: php zend-framework zend-form


    【解决方案1】:

    输入问题会带来新的见解!

    问题出在我自己的代码中。在某些时候,我重定向到同一页面。重定向意味着 $_POST 数据也被清除。当然,这些数据不会与新页面请求一起使用。

    因此,如果有人在 Zend Framework 中遇到此问题,即您在提交表单时没有收到 POST 或 GET 数据,那么您需要检查您的重定向。

    【讨论】:

      猜你喜欢
      • 2014-07-03
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多