seu_barriga
seu_barriga

Reputation: 11

Zend framework - form not rendering

I'm just starting to use Zend Framework and was following the quick start documentation for the latest version (1.11.10). Everything was going just fine, but when I placed the form code and ran the application, the form did not render. My code is exactly like http://framework.zend.com/manual/en/learning.quickstart.create-form.html

On the view, I can dump the form just fine with var_dump($this->form); I've tried echo $this->form(), echo $this->form->render(), but nothing appeared... What could it be?

Upvotes: 1

Views: 2669

Answers (4)

Shadi Hariri
Shadi Hariri

Reputation: 197

I had a problem like that (exact same form, since it is eclipse example)
My problem was due to misunderstanding. Since I thought that I have to directly access to the view script. I entered in the browser something like: hostname/application/view/script/something.php
But in zend all accesses should be through public folder. You have to access to the view like this: hostname/app_name/public/guestbook
hope that would help you

Upvotes: 0

madeinukraine
madeinukraine

Reputation: 41

This problem can occur when Zend can't find the template file for an element. Look at following code:

 $element->setDecorators(array(
            array('ViewScript',
                array(
                    'viewScript' => 'directory/_input.phtml'
                )
            )
        ));

The file _input.phtml must be in the right folder for this Controller. Otherwise Zend can't find the template for input and can't successfully render your element and will not show anything.

Upvotes: 2

Francis Yaconiello
Francis Yaconiello

Reputation: 10919

Ok so i tried your code, and it worked for me no problem. Here is everything:

Controller

<?php
class IndexController extends Zend_Controller_Action
{
    public function myTestAction()
    {
        $form = new Form_Guestbook();

        // ... processing logics
    if($this->getRequest()->isPost())
    {
    if($form->isValid($this->getRequest()->getPost()))
    {
        var_dump($form->getValues());
    }
    }
        $this->view->assign('form', $form);
    }
}

Form

        <?php
        class Form_Guestbook extends Zend_Form
        {
            public function init()
            {
                // Set the method for the display form to POST
                $this->setMethod('post');

                // Add an email element
                $this->createElement('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,
                ));
            }
        }
    ?>

View

<?php echo $this->form->render(); ?>

can be seen on: http://yaconiello.com/index/my-test

If this isnt working for you, you may be having a configuration error.

Upvotes: 1

Chris Laplante
Chris Laplante

Reputation: 29658

Make sure you pass the form to the view from the controller.

In your action handler:

$this->view->form = $my_form;

In your view:

echo $this->form;

I suspected that this was the cause of your problem because Zend Framework doesn't complain if you try to echo a parameter that doesn't exist. (i.e. echo $this->some_fake_parameter won't do anything)

Upvotes: 1

Related Questions