0

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

$this->form->setAction($this->url(array('action' => 'index')));
echo $this->form;?>

here is my index controller

<?php

class IndexController extends Zend_Controller_Action
{
   
        public function indexAction()
    {//echo "login controller";
	 $form = new Default_Form_Index();
         $this->view->form = $form;

       //	echo "Our home page stuff";
    }

and here is my form page

class Default_Form_Index extends Zend_Form
{
    /**
     * init() is the initialization routine called when Zend_Form objects are
     * created. In most cases, it make alot of sense to put definitions in this
     * method, as you can see below.  This is not required, but suggested.
     * There might exist other application scenarios where one might want to
     * configure their form objects in a different way, those are best
     * described in the manual:
     *
     * @see    http://framework.zend.com/manual/en/zend.form.html
     * @return void
     */
    public function init()
    {
        // Set the method for the display form to POST
        $this->setMethod('post');

        // Add an email element
        $this->addElement('text', 'email', array(
            'label'      => 'Your email address:',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array(
                'EmailAddress',
            )
        ));

        // Add the comment element
        $this->addElement('textarea', 'comment', array(
            'label'      => 'Please Comment:',
            'required'   => true,
            'validators' => array(
                array('validator' => 'StringLength', 'options' => array(0, 20))
                )
        ));

        // Add a captcha
        $this->addElement('captcha', 'captcha', array(
            'label'      => 'Please enter the 5 letters displayed below:',
            'required'   => true,
            'captcha'    => array('captcha' => 'Figlet', 'wordLen' => 5, 'timeout' => 300)
        ));

        // Add the submit button
        $this->addElement('submit', 'submit', array(
            'ignore'   => true,
            'label'    => 'Sign Guestbook',
        ));

        // And finally add some CSRF protection
        $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));
    }
}

Kindly make a note to me if anyone can solve this issue

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by sugumarclick
1

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

class IndexController extends Zend_Controller_Action
{
  public function indexAction()
  {
    $form = new Default_Form_Index();
    $request = $this->getRequest();

    //Check if we have post data
    if( $request->isPost() )
    {
      //If the form is NOT valid redisplay it with errors
      if( !$form->isValid($request->getPost() ) )
      {
        $this->view->form = $form;				           		
        return;
      }

      //Form passes validation and filtering
      //Do something with the data.
      $data = $form->getValues();

      //Maybe a redirect or change what view is getting rendered?

      //Just to stop execution after processing the form return;
      //return; 
    }

    //If no post data assume we're just displaying the form to the user
    $this->view->form = $form;
  }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.