Leszek
Leszek

Reputation: 182

Zend_Form - isPost() returns false

I want to write small register form. This is my form code:

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(),
            ]
        );

    }
}

This is my registerAction in appropriate Controller:

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

I don't know why isPost() method returns false. Any suggestions?

Upvotes: 1

Views: 209

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

For good practice - change your logic as following below:

  1. create view file with name form.phtml and the following exemplary content:

    <h2>Please sign up:</h2>
    <?php echo $this->form ?>
    
  2. Modify your RegisterController.php in such way:

    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. At first index action must be called to show the register form to user. Also make sure that submit button on register form indicates to register action. I hope, you have added submit button in such way: $form->addElement('submit', 'register', array('label' => 'Sign up'));

  4. Check results

Upvotes: 1

Related Questions