Zend Form getValue. Need someone to explain

zend-form

I'm just working with the Zend_Form in Zend Framework and came across something pretty weird.

I have the following inside my loginAction

$form = new Application_Model_FormLogin();

if ($this->getRequest()->isPost()) {

      $email = $form->getValue('email');
      $pswd = $form->getValue('pswd');

      echo "<p>Your e-mail is {$email}, and password is {$pswd}</p>";

}

Which when submitted only outputs

Your e-mail is, and password is

So I checked to see what's going on with print_r ,

print_r($form->getValues());
print_r($_POST);

Which displayed the following,

Array ( [email] => [pswd] => ) Array ( [email] => asd [pswd] => asd [submit] => Login )

So the forms values array has both values as null and the global post array had the correct values. Now I can't work out the problem?

Now I did manage to fix the problem, but I need help understanding why this works? All I did was change the loginAction to this.

$form = new Application_Model_FormLogin();

if ($this->getRequest()->isPost()) {


        //Added this in
        if ($form->isValid($this->_request->getPost())) {

            $email = $form->getValue('email');
            $pswd = $form->getValue('pswd');

            echo "<p>Your e-mail is {$email}, and password is {$pswd}</p>";

        }

}

I don't get how this made it work? Considering there is no validation on the fields?

Any thoughts? All I can think is maybe I have something setup weird in my server configuration?

Thanks

Best Answer

You didnt load the Values in your form object.

Normaly you check if the form is valid and for this load it with the post data, in the next step you can use getValue() to get the (filtered) value from the form.

if($this->getRequest()->isPost()) {
    $form = new My_Form();
    if($form->isValid($this->getRequest()->getPost())){
        echo $form->getValue('fieldname');        
    }
}
Related Topic