开发者

On submit the form don't display its POST data

开发者 https://www.devze.com 2023-02-10 09:19 出处:网络
I have a little experience with Zend Framework, but I like to fiddle with it until it works. But now I cannot resolve this problem.

I have a little experience with Zend Framework, but I like to fiddle with it until it works. But now I cannot resolve this problem.

I have a form:

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

A view:

<?= $this->form ?>

<?= $this->postdata ?>

And a AccountController:

<?php

class AccountController extends Zend_Controller_Action
{

public function init()
{
    echo 'data:'.$this->getRequest()->getPost('emailAddress');
    /* Initialize action controller开发者_StackOverflow社区 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');
            }
        }

As you see I put in a comment: // THIS POINT IS NEVER REACHED. There are more functions in this controller, but those are not relevant for my problem.

Let explain it a bit more. The very strange behavior is that when I put data in my fields, $this->view->postdata = var_dump($this->getRequest()->getParams() returns no POST data. But when I put notting in the login form fields, then I see the POST data. Of course it is empty. Like this:

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)

Thus, //THIS POINT IS NEVER REACHED is actually reached when putting no data in the login form fields :-)

The question is, what are I am doing wrong? Do I deal with the Zend_Controller_Request_Http in the wrong way?

If you need more info, I should give it.


Typing down the problem gives new insights!

The problem is in my own code. On some point I redirect to the same page. A redirect means that the $_POST data is also cleared. Those data does - of course - not go with the new page request.

So if someone encounter this problem with Zend Framework, namely you get no POST or GET data on submit of your form, then you need to check your redirects.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号