Validation in Zend Framework Not working in Index Controller

Thread Solved

Join Date: Jun 2009
Posts: 17
Reputation: sugumarclick is an unknown quantity at this point 
Solved Threads: 0
sugumarclick sugumarclick is offline Offline
Newbie Poster

Validation in Zend Framework Not working in Index Controller

 
0
  #1
Oct 29th, 2009
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.
  1. <?php
  2.  
  3. $this->form->setAction($this->url(array('action' => 'index')));
  4. echo $this->form;?>
here is my index controller
  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
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 149
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster
 
1
  #2
Oct 29th, 2009
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.

  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. }
If you're question/problem is solved don't forget to mark the thread as Solved!

-- Code I post is usually but not always tested. If it is tested it will be against 5.2.12 or 5.3.1
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 17
Reputation: sugumarclick is an unknown quantity at this point 
Solved Threads: 0
sugumarclick sugumarclick is offline Offline
Newbie Poster
 
0
  #3
Oct 29th, 2009
It works. . .Thankyou
[QUOTE=mschroeder;1030904]
Reply With Quote Quick reply to this message  
Reply

Tags
form, validator, zend

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for form, validator, zend
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC