Zend_Form - isPost()返回false

时间:2016-01-31 18:52:43

标签: php forms zend-framework

我想写小注册表。 这是我的表单代码:

class Application_Form_Register extends Zend_Form
{

    public function init()
    {
        $this->setMethod('post');

        $this->addElements(
            [
                $this->getNameFirst(),
                $this->getNameLast(),
                $this->getEmail(),
                $this->getPassword(),
                $this->getPasswordConfrim(),
                $this->getSex(),
                $this->getDateBirth(),
                $this->getAddressStreet(),
                $this->getAddressStreetHn(),
                $this->getAddressStreetAn(),
                $this->getCityCode(),
                $this->getCityName(),
                $this->getSubmitButton(),
            ]
        );

    }
}

这是我在适当的Controller中的registerAction:

public function registerAction()
    {
        $form = new Application_Form_Register();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                var_dump($values);die();
            }
        }
    }

我不知道为什么isPost()方法返回false。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

为了良好实践 - 按照以下方式更改逻辑:

  1. 创建名称为form.phtml的视图文件以及以下示例内容:

    <h2>Please sign up:</h2>
    <?php echo $this->form ?>
    
  2. 以这种方式修改RegisterController.php

    class RegisterController extends Zend_Controller_Action
    {
        public function getForm()
        {
            // creating form
            $form = new Application_Form_Register();
            return $form;
        }
    
        public function indexAction()
        {
            // rendering form.phtml
            $this->view->form = $this->getForm();
            $this->render('form');
        }
    
        public function registerAction()
        {
            if (!$this->getRequest()->isPost()) {
                return $this->_forward('index');
            }
            $form = $this->getForm();
            if (!$form->isValid($_POST)) {
                // if form values aren't valid, output form again
                $this->form = $form;
                return $this->render('form');
            }
    
            $values = $form->getValues();
            //var_dump($values);die();
            // authentication...
        }
    }
    
  3. 必须首先调用index操作以显示注册表单 用户。还要确保注册表单上的submit按钮表示 到register行动。我希望,你已经添加了提交按钮 方式:$form->addElement('submit', 'register', array('label' => 'Sign up'));

  4. 检查结果

相关问题