1

I am working with zend just recently. I discovered this ViewScript decorator for form and I find it a best alternative of using the classic Zend Form Decorators. But I have problem on displaying the form. I got the code working but no display I get from the view.

Here are my codes:

Form:

class Application_Form_Registration extends Zend_Form
{
    public function init()
    {   
        $username = new Zend_Form_Element_Text("username");
        $submit = new Zend_Form_Element_Submit("submit");
        $this->setAction("/test.php");
        $this->setMethod("get");
        $this->addElements(array($username, $submit));
        $this->setElementDecorators(array(
          array('ViewScript', array(
            'viewScript'=>'test.phtml'
          ))
        ));
    }
}

Controller:

class IndexController extends Zend_Controller_Action
{
    public function init()
    {
    }

    public function indexAction()
    {
        $form = new Application_Form_Registration();
        $this->view->form = $form;

    }
}

test.phtml (My ViewScript)

<form action="<?php $this->escape($this->form->getAction()); ?>">
<div style="width: 100px; height: 100px; background: blue;">
    <?php echo $this->element->username; ?>
    <?php echo $this->element->submit; ?>
</div>
</form>

And my view (index.phtml)

<?php echo $this->form; ?>

Did I missed something and / or made wrong with the above code?

2 Answers 2

3

Replace

  $this->setElementDecorators(array(
              array('ViewScript', array(
                'viewScript'=>'test.phtml'
              ))
            ));

with

$this->setDecorators(array(
              array('ViewScript', array(
                'viewScript'=>'test.phtml'
              ))
            ));

You have bassically overrided default decorator 'ViewHelper' hence there is nothing to show .

Both form (html form tag) and form elements (input type text,radio etc) uses decorators to display themselves . By calling setElementDecorators on Zend_Form instance you are overriding form elements decorators not form decorators for that we need to use setDecorators instead .

Sign up to request clarification or add additional context in comments.

2 Comments

thanks buddy.. the code works for me.. but what totally is the difference between the two?
ahhh.. i got it.. so when we use the setElementDecorators, we overrode everything that surrounds it.. now i got it.. thanks.. :D
1

believe it or not you access getAction in the partial using element->getAction, and don't forget to echo it:

//test.php
<form action="<?php echo $this->escape($this->element->getAction()); ?>">
<div style="width: 100px; height: 100px; background: blue;">
    <?php echo $this->element->username->render(); ?>
    <?php echo $this->element->submit->render(); ?>
</div>
</form>

and the view would be:

//index.phtml
<?php echo $this->form ?>

5 Comments

thanks rocky... I've done what you instructed but still no form rendered / displayed.. have any idea?
The default location for a viewscript partial would be /view/scripts/test.php. It would be the same for any module.
yes.. I understand it.. currently I am not working with module yet but looking ahead for it. hmmm.. I'm just testing it in the default location.
check the edit and see if it works. I added ->render() to the elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.