944,045 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3638
  • PHP RSS
Oct 29th, 2009
0

Validation in Zend Framework Not working in Index Controller

Expand Post »
Hi All,
I went Through the tutorial of zend framework. . I can able to validate the forms outside the index controller. But the same form which is called in index controller is not validating. I searched in web but none helped me. .
I wanna know why the form is not validating in index controller.
here is my view page.
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $this->form->setAction($this->url(array('action' => 'index')));
  4. echo $this->form;?>
here is my index controller
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. class IndexController extends Zend_Controller_Action
  4. {
  5.  
  6. public function indexAction()
  7. {//echo "login controller";
  8. $form = new Default_Form_Index();
  9. $this->view->form = $form;
  10.  
  11. // echo "Our home page stuff";
  12. }
and here is my form page
PHP Syntax (Toggle Plain Text)
  1. class Default_Form_Index extends Zend_Form
  2. {
  3. /**
  4.   * init() is the initialization routine called when Zend_Form objects are
  5.   * created. In most cases, it make alot of sense to put definitions in this
  6.   * method, as you can see below. This is not required, but suggested.
  7.   * There might exist other application scenarios where one might want to
  8.   * configure their form objects in a different way, those are best
  9.   * described in the manual:
  10.   *
  11.   * @see http://framework.zend.com/manual/en/zend.form.html
  12.   * @return void
  13.   */
  14. public function init()
  15. {
  16. // Set the method for the display form to POST
  17. $this->setMethod('post');
  18.  
  19. // Add an email element
  20. $this->addElement('text', 'email', array(
  21. 'label' => 'Your email address:',
  22. 'required' => true,
  23. 'filters' => array('StringTrim'),
  24. 'validators' => array(
  25. 'EmailAddress',
  26. )
  27. ));
  28.  
  29. // Add the comment element
  30. $this->addElement('textarea', 'comment', array(
  31. 'label' => 'Please Comment:',
  32. 'required' => true,
  33. 'validators' => array(
  34. array('validator' => 'StringLength', 'options' => array(0, 20))
  35. )
  36. ));
  37.  
  38. // Add a captcha
  39. $this->addElement('captcha', 'captcha', array(
  40. 'label' => 'Please enter the 5 letters displayed below:',
  41. 'required' => true,
  42. 'captcha' => array('captcha' => 'Figlet', 'wordLen' => 5, 'timeout' => 300)
  43. ));
  44.  
  45. // Add the submit button
  46. $this->addElement('submit', 'submit', array(
  47. 'ignore' => true,
  48. 'label' => 'Sign Guestbook',
  49. ));
  50.  
  51. // And finally add some CSRF protection
  52. $this->addElement('hash', 'csrf', array(
  53. 'ignore' => true,
  54. ));
  55. }
  56. }


Kindly make a note to me if anyone can solve this issue
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sugumarclick is offline Offline
38 posts
since Jun 2009
Oct 29th, 2009
1
Re: Validation in Zend Framework Not working in Index Controller
if you're making the form submit to itself then you need to make the index action in the index controller look for post data so it knows it needs to validate the form.

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. class IndexController extends Zend_Controller_Action
  4. {
  5. public function indexAction()
  6. {
  7. $form = new Default_Form_Index();
  8. $request = $this->getRequest();
  9.  
  10. //Check if we have post data
  11. if( $request->isPost() )
  12. {
  13. //If the form is NOT valid redisplay it with errors
  14. if( !$form->isValid($request->getPost() ) )
  15. {
  16. $this->view->form = $form;
  17. return;
  18. }
  19.  
  20. //Form passes validation and filtering
  21. //Do something with the data.
  22. $data = $form->getValues();
  23.  
  24. //Maybe a redirect or change what view is getting rendered?
  25.  
  26. //Just to stop execution after processing the form return;
  27. //return;
  28. }
  29.  
  30. //If no post data assume we're just displaying the form to the user
  31. $this->view->form = $form;
  32. }
  33. }
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
624 posts
since Jul 2008
Oct 29th, 2009
0
Re: Validation in Zend Framework Not working in Index Controller
It works. . .Thankyou
[QUOTE=mschroeder;1030904]
Reputation Points: 10
Solved Threads: 0
Light Poster
sugumarclick is offline Offline
38 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Function within a loop
Next Thread in PHP Forum Timeline: str_replace help needed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC