Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework Custom Forms with viewScript

I am having some problems working out how to use custom forms in Zend Framework.

I have followed various guides but none seem to work. Nothing at all gets rendered.

Here is the bits of code that I am trying to use (All code below is in the default module). I have simplified the code to a single input for the test.

applications/forms/One/Nametest.php

class Application_Form_One_Nametest extends Zend_Form {

    public function init() {

        $this->setMethod('post');

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Box Name')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Submit Message');
        $submit->setAttrib('id', 'submitbutton');
        $submit->setAttrib('class', 'bluebutton');

        $this->addElements(array($name, $submit));
    }

}

application/views/scripts/one/formlayout.phtml

<form action="<?= $this->escape($this->form->getAction()) ?>" method="<?= $this->escape($this->form->getMethod()) ?>">

    <p>
        Please provide us the following information so we can know more about
        you.
    </p>

    <? echo $this->element->name ?>
    <? echo $this->element->submit ?>

</form>

application/controllers/IndexController.php

public function formtestAction() {
    $form = new Application_Form_One_Nametest();
    $form->setDecorators(array(array('ViewScript', array('viewScript' => 'one/formlayout.phtml'))));

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

application/views/scripts/index/formtest.phtml

<h1>Formtest</h1>
<?
echo $this->form;       
?>

The above code does not throw any errors or render any part of formlayout.phtml including the form tags or text between the p tags.

Can anybody tell me what I might be doing wrong?

like image 481
someuser Avatar asked Dec 09 '11 05:12

someuser


2 Answers

I think the problem is your form element's decorator. You should set the decorator to ViewHelper and Error only. It works for me at least.

Here is the code I used and it should work

applications/forms/Form.php

class Application_Form_Form extends Zend_Form {

public function loadDefaultDecorators() {
    $this->setDecorators(
        array(
            array(
                'ViewScript',
                array(
                    'viewScript' => 'index/formlayout.phtml',
                )
            )
        )
    );
}

    public function init() {
        $this->setAction('/action');
        $this->setMethod('post');

        $this->addElement('text', 'name', array(
            'decorators' => array('ViewHelper', 'Errors')
        ));
    }
}

application/views/scripts/index/formlayout.phtml

<form action="<?php echo $this->element->getAction(); ?>" method="<?php echo $this->element->getMethod(); ?>">
  <div>
    <label for="name">Box Name</label>
    <?php echo $this->element->name; ?>
  </div>

  <input type="submit" value="Submit Message" id="submitbutton" class="bluebutton">
</form>

application/views/scripts/index/index.phtml

<!-- application/views/scripts/index/index.phtml -->
<?php echo $this -> form; ?>

application/controllers/IndexController.php

public function indexAction() {
    $form = new Application_Form_Form();
    $this -> view -> form = $form;
}
like image 175
Bing Avatar answered Nov 02 '22 19:11

Bing


Here is a very simple example to get you going adapted from this article.

The form:-

class Application_Form_Test extends Zend_Form
{
    public function init()
    {
        $this->setMethod('POST');
        $this->setAction('/');
        $text = new Zend_Form_Element_Text('testText');

        $submit = new Zend_Form_Element_Submit('submit');

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

        $this->addElements(array($text, $submit));
        $this->setElementDecorators(array('ViewHelper'));
    }
}

The order in which setDecorators(), addElements() and setElementDecorators() are called is very important here.

The view script _form_test.phtml can be called anything you like, but it needs to be in /views/scripts so that it can be found by the renderer.

/views/scripts/_form_test.phtml would look something like this:-

<form id="contact" action="<?php echo $this->element->getAction(); ?>" 
      method="<?php echo $this->element->getMethod(); ?>">

<p>
Text<br />
<?php echo $this->element->testText; ?>
</p>

<p>
<?php echo $this->element->submit ?>
</p>

</form>

You instantiate the form, pass it to the view and render it as usual. The output from this example looks like this:-

<form id='contact' action='/' method='post'>
    <p>
        Text<br />
        <input type="text" name="testText" id="testText" value=""></p>
    <p>

    <input type="submit" name="submit" id="submit" value="submit"></p>
</form>

That should be enough to get you started creating your own forms.

like image 23
vascowhite Avatar answered Nov 02 '22 18:11

vascowhite