phpzend-frameworkzend-formzend-controller

Contact Us Form not working under Zend Framework


I started learning Zend so this is the link i followed for the contact us page -

My Zend Version - Zend Framework v1.11.11

http://www.tutorial-portal.com/tutorial/show/id/27

I created form using command line - zf create form Contact which successfully created folder forms under which Contact.php File. Code in Contact.php is as follows -

<?php

class Application_Form_Contact extends Zend_Form
{

    public function init()
    {
        /* Form Elements & Other Definitions Here ... */
        $this->setmethod('post');
        $this->setName('contact-form');

        $this->addElement('text', 'name', array(
                            'label' => 'Please enter your name',
                            'required' => true,
                         ));


        $this->addElement('text', 'email', array(
                            'label' => 'Please enter email address',
                            'required' => true,
                            'validators' => array('EmailAddress'),
                         ));

        $this->addElement('textarea', 'message', array(
                            'label' => 'Please enter your message',
                            'required' => true,
                            'validators' => array( array('validator' => 'StringLength', 'options' => array(0, 20) )
                         ));

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

        $this->addElement('submit', 'submit', array(
                            'ignore'   => true,
                            'label'    => 'Send Message',
                        ));

        $this->addElement('hash', 'csrf', array(
                            'ignore' => true,
                        ));
    }               
}

Now I created a controller ContactController.php under C:\xampp\htdocs\projectone\application\controllers. Code in this controller goes like this -

<?php

class ContactController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        //$form  = new Application_Form_Contact();
        //$this->view->form = $form;

    }

    public function indexAction()
    {
        // action body
        // Create form instance
        $form = new Application_Form_Contact();

        /**
         * Get request
         */
        $request = $this->getRequest();
        $post = $request->getPost(); // This contains the POST params

        /**
         * Check if form was sent
         */
        if ($request->isPost()) {
            /**
             * Check if form is valid
             */
            if ($form->isValid($post)) {
                // build message
                $message = 'From: ' . $post['name'] . chr(10) . 'Email: ' . $post['email'] . chr(10) . 'Message: ' . $post['message'];
                // send mail
                mail('contact@yourwebsite.com', 'contact: ' . $post['subject'], $message);
            }
        }

        // give form to view (needed in index.phtml file)
        $this->view->form = $form;

    }

}

Code in index.phtml under the folder structure C:\xampp\htdocs\projectone\application\views\scripts\contact goes like this - echoing the code in php tags - echo $this->form

But this is not working at all :( really unable to locate what i am doing wrong.

Its only showing a blank page, and nothing even simple html content.

This is the URL im using, although rest controllers working fine -

http://localhost/projectone/public/contact

PS- Since I am a starter in Zend, I dont know where & how to look for Zend errors, let me know this also :)


Solution

  • After performing various heuristics, and checking the code at various levels I am able to debug my code. Problem actually was in this LOC -

    BUG -

        $this->addElement('textarea', 'message', array(
                            'label' => 'Please enter your message',
                            'required' => true,
                            'validators' => array( array('validator' => 'StringLength', 'options' => array(0, 20) )
                         ));
    

    REMOVE

    'validators' => array( array('validator' => 'StringLength', 'options' => array(0, 20) )
    

    and all goes well like a charm.