开发者

Zend Form getValue. Need someone to explain?

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_Mo开发者_开发技巧del_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


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


isValid() is what actually populates the fields in your form object, until you do that the values do not exist in your form object yet.

modifying your original code would be as simple as this

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

  //your $form object has none of your POSTed values
  $form->isValid($this->getRequest()->getPost())

  //now your form object has the POSTed values and you can access them
  $email = $form->getValue('email');
  $pswd  = $form->getValue('pswd');

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

}

This skims over it extremely lightly http://framework.zend.com/manual/1.11/en/zend.form.quickstart.html#zend.form.quickstart.validate

Consider this example also and it might make more sense. Here you just grab the values from the POST.

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

      $email    = $this->getRequest()->getPost('email');
      $password = $this->getRequest()->getPost('password');

      echo "<p> Your email is $email and your password is $password </p>";

    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜