views:

864

answers:

2

(I follow this post to got the logic how to use Zend_Form without using decorators.)

within the action I create Zend_Form object and assigned the form to the view before rendering it:

public function indexAction()
{
        $form = new Zend_Form();
        $form->setAction('process')
            ->setMethod('post');

        $username = $form->createElement('text','username');
        $username->addValidator('alnum')
        ->setRequired(true)
        ->addFilter('StringtoLower');

        $description = $form->createElement('textarea','description');
        $description->addValidator('alnum')
                    ->setRequired(false)
                    ->addFilter('StringtoLower');

        $submit = $form->createElement('submit','Submit');

        $form->addElement($username);
        $form->addElement($description);
        $form->addElement($submit);

        $this->view->form = $form;
        $this->view->render('myForm/index.phtml'); //myForm is the actionController

    }

Then I add following code to it's view script (index.phtml)

<form>
    <table>
        <tr>
            <td><label>Username</label></td>
            <td><?php echo $this->form->getElement('username')->renderElement(); ?></td>
        </tr>
        <tr>
            <td><label>Description</label></td>
            <td><?php echo $this->form->getElement('description')->renderElement(); ?></td>
        </tr>
        <tr>
            <td></td>
            <td><?php echo $this->form->getElement('submit')->renderElement(); ?></td>
        </tr>
    </table>
</form>

But it's get an error

Message: Decorator by name Element does not exist

I didn't use decorators,What is the problem of my code.

This is the complete custom error message

Application error
Exception information:

Message: Decorator by name Element does not exist
Stack trace:

#0 [internal function]: Zend_Form_Element->__call('renderElement', Array)
#1 C:\Program Files\Zend\Apache2\htdocs\Demo1\application\views\scripts\myForm\index.phtml(5): Zend_Form_Element_Text->renderElement()

#2 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\View.php(108): include('C:\Program File...')
#3 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\View\Abstract.php(833): Zend_View->_run('C:\Program File...')

#4 C:\Program Files\Zend\Apache2\htdocs\Demo1\application\controllers\MyFormController.php(38): Zend_View_Abstract->render('myForm/index.p...')
#5 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\Controller\Action.php(513): myFormController->indexAction()

#6 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\Controller\Dispatcher\Standard.php(289): Zend_Controller_Action->dispatch('indexAction')
#7 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\Controller\Front.php(946): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))

#8 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\Application\Bootstrap\Bootstrap.php(77): Zend_Controller_Front->dispatch()
#9 C:\Program Files\Zend\Apache2\htdocs\Demo1\library\Zend\Application.php(335): Zend_Application_Bootstrap_Bootstrap->run()

#10 C:\Program Files\Zend\Apache2\htdocs\Demo1\public\index.php(27): Zend_Application->run()
#11 {main}

How I solve this problem without using decorators

+1  A: 

You actually need to remove the default decorators, and add the ViewScript Decorator (along maybe the Error Decorator) and then render it, take a look at this article.

Simple example:

class Demo_Form extends Zend_Form {
public function init() {
 $this->addElement('text', 'username', array( 'decorators' => array( 'ViewHeper', 'Errors' ) ));
 $this->addElement('text', 'lastname', array( 'decorators' => array( 'ViewHeper', 'Errors' ) ));
 $this->setDecorators(array( array('ViewScript' => array( 'script' => 'demoForm.phtml'))));
}
}

And with a view script (demoForm.phtml):

<form action="<?php echo $this->escape($this->form->getAction() ?>" method="<?php echo $this->escape($this->form->getMethod() ?>">
<table>
 <tr>
  <td>User Name:</td>
  <td><?php echo $this->form->username; ?></td>
 </tr>
 <tr>
  <td>Last Name:</td>
  <td><?php echo $this->form->lastname; ?></td>
 </tr>
</table>
</form>

You can have complete control of how and where to render your elements, then on your final view (the one from the controller) just print your form the same: form; ?> to render the viewScript onto your View.

Chris
Right. When you don't set the ViewScript decorator, it expects you to render the form as one, not as individual elements.
jimyi
I don't want to use decorators,It's use too complicated.Anyone have a idea about the this error when using this way."Message: Decorator by name Element does not exist".I have used same view script(which for controller) for the HTML form.Is it cause any error? Thanks for answering
neobeacon
And the topic has spelling mistake.It's must be "Zend_Form with xhtml scripts".Sorry
neobeacon
You are going to use just two decorators, if you look at the article I posted, you only need to setup the script decorator to render your form (and the Error to display errors).
Chris
I can not understand what you are saying exactly,I read Decorators with Zend_Form article.But My actual form is more complicated than this,it's with 4 tables in many places in page.So I want to render individual form elements in view.Thanks.
neobeacon
Ok, I've edited and attached a simple example so you can see how to render the elements where you want.
Chris
Thank you very much for still with me.I had given up this form stuff.But now I decide to do it back with your way.I hope it will work. I looked the post but I didn't get good understand about decorators.still I have no idea about "array( 'decorators' => array( 'ViewHeper', 'Errors' )" and "array('ViewScript' => array( 'script' => 'demoForm.phtml'))" and the file structure of them.So I must get good understand about decorators first.Thank you very much again.
neobeacon
To put it simply, decorators are functions that "decorate" content, the reason to use ViewHelper and Error Decorators is that those are needed to render the element, and error messages from the validator. The ViewScript validator is used to render the form onto a view script. Those are the basic stuff so you can display the form.In the link I gave you you can see how Decorators work more correctly, they basically wrap up content based on the order of how they are registered.
Chris
Thanks a lot.Where do I want to put "demoForm.phtml",I use form directory in application directory to store my forms.
neobeacon
And also I use a library for load the correct view script for action controllers.can't I use my view script(for DemoFormAction) as demoForm.phtml
neobeacon
If you supply only demoForm.phtml it will search in the current path, so if it's in ahother path use the ful path.The idea is to have the viewscript only for the form and then inyect it on your view from your controller.
Chris
Got the idea.Thanks in deep
neobeacon
A: 
P.M