我是Zend Framework的初学者。 我猜我的问题很基本......但我不能自己解决。
在 indexAction 中, $ request-> isPost()始终为false。 发生了什么事?
EntryController ::的indexAction
public function indexAction() {
$form = new AgreementForm();
$form->get('submit')->setValue('Go Entry Form');
$request = $this->getRequest();
if ($request->isPost()) {
var_dump('//// $request->isPost() is true //////');
if ($form->get('agreementCheck')) {
// Redirect to list of entries
return $this->redirect()->toRoute('entry');
} else {
return array('form' => $form);
}
} else {
var_dump('//// $request->isPost() is false //////');
return array('form' => $form);
}
}
index.phtml 中的 表单
AgreementForm 。
http://zend-form-generator.123easywebsites.com/formgen/create
如下。 请告诉我一些提示。 更新
在Developer Tools的分析结果中,POST和GET同时工作。
更新
路由器定义@ module.config.php就是这个。
使用代码生成器生成<?php
$form = $this->form;
$form->setAttribute('action', $this->url('entry', array('action' => 'index')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formCheckbox($form->get('agreementCheck'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
class AgreementForm extends Form {
public function __construct($name = null) {
parent::__construct('');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'agreementCheck',
'type' => 'Zend\Form\Element\MultiCheckbox',
'attributes' => array(
'required' => 'required',
'value' => '0',
),
'options' => array(
'label' => 'Checkboxes Label',
'value_options' => array(
'0' => 'Checkbox',
),
),
));
$this->add(array(
'name' => 'csrf',
'type' => 'Zend\Form\Element\Csrf',
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
'router' => array(
'routes' => array(
'entry' => array(
'type' => 'segment',
'options' => array(
'route' => '/entry[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Entry\Controller\Entry',
'action' => 'index',
),
),
),
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Entry\Controller\Entry',
'action' => 'index',
),
),
),
),
),
答案 0 :(得分:1)
有些事情是错的:
在表单类中添加csrf元素,但不在视图中呈现它。这将导致验证错误。所以你需要将它添加到你的视图中:
echo $ this-&gt; formHidden($ form-&gt; get('csrf'));
您正在向表单添加Multicheckbox元素,但在您的视图中,您正在使用formCheckbox视图助手来呈现它。如果你真的想要一个Multicheckbox,那么你应该使用formMultiCheckbox帮助器来渲染它:
echo $ this-&gt; formMultiCheckbox($ form-&gt; get('agreementCheck'));
完成这些更改后,它应该可以正常工作。
编辑:您也可以将名称传递给表单构造函数:
parent::__construct('agreementform');
答案 1 :(得分:0)
我认为,你不需要明确说出
$form->setAttribute('action', $this->url('entry', array('action' => 'index')));
省略那条线,看看会发生什么。