Php Zend_Form-isPost()返回false
我想写一份小登记表。 这是我的表格代码:Php Zend_Form-isPost()返回false,php,forms,zend-framework,Php,Forms,Zend Framework,我想写一份小登记表。 这是我的表格代码: class Application_Form_Register extends Zend_Form { public function init() { $this->setMethod('post'); $this->addElements( [ $this->getNameFirst(), $t
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(),
]
);
}
}
这是我在相应控制器中的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。有什么建议吗?有关良好实践的建议-按以下方式更改您的逻辑:
form.phtml
的视图文件和以下示例性内容:
<h2>Please sign up:</h2>
<?php echo $this->form ?>
index
操作以向用户显示注册表表单
用户。还要确保登记表上的submit
按钮指示
注册操作。我希望,你已经添加了提交按钮在这样的网站
方式:$form->addElement('submit','register',array('label'=>'符号)
(上升),代码>
显示同一控制器中的
索引操作
内容为空。我没有在这个action中编写代码,以前是什么action呈现了您的表单?还是没有呈现?这张表格是我写的第一件事。我放在这里的代码几乎都是我项目中的代码。此外,我在registerAction视图中通过echo$this->form
呈现此表单。我知道文档向我们展示了此解决方案,但我的问题是“是否有任何方法可以在一个操作中显示和呈现表单?”。我知道这是可能的,但如何做到?您可以尝试将所有逻辑集中在索引操作中,并在提交按钮选项中更改目标:$form->addElement('submit','index',array('label'=>'Sign up')代码>确定。我注意到您的解决方案正在运行,但我不知道$form->addElement('submit','index',array('label'=>'Sign up'))之间有什么不同
和$submitButton=new Zend_Form_Element_按钮('submit')$submitButton->setLabel('Submit')$submitButton->setName(“寄存器”);返回$submitButton代码>
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...
}
}