2

I am a Zend Framework beginner. I guess My question is very basic... but I can't solve it by myself.

In indexAction, $request->isPost() is always false. What is happening?

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); } } 

form in index.phtml

<?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(); ?> 

AgreementForm is generated using code generator. http://zend-form-generator.123easywebsites.com/formgen/create as below.

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', ), )); } } 

Please tell me some hints.

update: In the result of analysis by Developer Tools, POST and GET works at the same time. enter image description here

update: router definition @module.config.php is this.

'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', ), ), ), ), ), 
12
  • check your HTML source to ensure the form method is POST and check the HTTP method after submitting with Firebug/Chrome debugger. Commented Aug 3, 2013 at 17:07
  • @BramGerritsen I make sure that form method is POST. But when submitting it, at the same time, Get method is runnning... By the way, index.phtml is defined as home in module.config.php Commented Aug 3, 2013 at 17:18
  • I wanna see your routes i.e module.config.php Commented Aug 4, 2013 at 5:18
  • @noobie-php Thank you for comment. I added router setting. Commented Aug 4, 2013 at 8:04
  • May i ask what exactly the EXPECTED result and what the CURRENT result is? Because no matter what, you'll always end up with a Form being displayed! In success-case without data, on Failure case without Commented Aug 4, 2013 at 8:23

2 Answers 2

1

A few things are wrong:

  1. In the form class you add a csrf element, but you don't render it in the view. This will cause a validation error. So you need to add this to your view:

    echo $this->formHidden($form->get('csrf'));

  2. You're adding a Multicheckbox element to the form, but in your view you're using the formCheckbox view helper to render it. If you really want a Multicheckbox then you should render it with the formMultiCheckbox helper:

    echo $this->formMultiCheckbox($form->get('agreementCheck'));

After these changes it should work.

Edit: Also you may want to pass a name to the form constructor:

parent::__construct('agreementform'); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I tried following your answer but situation did not change. POST and GET method works at the same time when submitting. I guess this is the problem.
I think the problem is in the form action. Check the rendered html of the form and in particular the form action attribute.
0

I think, you do not need to explicitly say

$form->setAttribute('action', $this->url('entry', array('action' => 'index'))); 

Omit that line, and see what happens.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.