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?
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
application/views/scripts/index/formlayout.phtml
application/views/scripts/index/index.phtml
application/controllers/IndexController.php
Usually, if you don't see anything on the screen it means that some sort of error happened. Maybe you have errors turned off or something, maybe not. I'm just trying to give you ideas.
The only things I could spot where the following. In the below code, you have to still specify the form when trying to print out the elements.
As vascowhite's code shows, once you are inside the viewscript, the variable with the form is called element. The viewscript decorator uses a partial to do the rendering and thus it creates its own scope within the viewscript with different variable names.
So, although in your original view it was called $form, in the viewscript you'll have to call it element.
Also, maybe it was copy/paste haste, but you used
<? ?>
tags instead of<?= ?>
or<?php ?>
tags. Maybe that caused some error that is beyond parsing and that's why you got no output.Here is a very simple example to get you going adapted from this article.
The form:-
The order in which
setDecorators()
,addElements()
andsetElementDecorators()
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:-You instantiate the form, pass it to the view and render it as usual. The output from this example looks like this:-
That should be enough to get you started creating your own forms.