Zend Form is one of the most powerful tools of Zend Framework. One of the most common misconceptions is that Zend Form cannot be rendered in it’s own individual parts. The way to fully customise a form is by using the using the Zend Form ViewScript Decorator.
Form File
forms/MyLoginForm.php
class Form_MyLoginForm extends Zend_Form { public function init() { $this->setDisableLoadDefaultDecorators(true); $this->setDecorators(array( array('ViewScript', array('viewScript' => 'form/_form.phtml')), 'Form' )); $this->setMethod('post'); $this->setAction(''); $this->addElement('text', 'username', array( 'decorators' => array( 'ViewHelper' ), )); $this->addElement('password', 'password', array( 'decorators' => array( 'ViewHelper' ), )); $this->addElement('submit', 'submit-button', array( 'decorators' => array( 'ViewHelper' ), 'label' => 'Submit' )); } }
Form View Script
application/views/scripts/form/_form.phtml
<div style="padding: 8px;">
<table style="width: 100%;">
<tbody>
<tr>
<td>Username:</td>
<td><?=$this->getElement('username'); ?></td>
</tr>
<tr>
<td>Password:</td>
<td><?=$this->getElement('password'); ?></td>
</tr>
</tbody>
</table>
<?=$this->element->getElement('submit-button'); ?>
</div>Controller
application/controllers/IndexController.php
class IndexController extends Zend_Controller_Action { public function indexAction() { $this->view->loginForm = new Form_MyLoginForm(); } }
Controller View Script
application/views/scripts/index/index.phtml
<h2>Login Form</h2> <?php echo $this->loginForm; ?>
Thanks for this post =D
However, I was not able to display the form.
I only got the “Login Form” in the index.phtml.
It seems like it could not find the path of the _form.phtml because when I tried to comment out the line setting the ViewScript path, it displayed the text box.
I even tried to force the path like so but it did not give me anything.
$decorPath = APPLICATION_PATH. ‘/form/_form.phtml’;
$this->setDecorators(array(
array(‘ViewScript’, array(‘viewScript’ => $decorPath )),
‘Form’
));
Please help.
Hi Joey,
That is unlikely to work because the decorator is using a path prefix of your standard views/scripts path. In a standard installation, the path prefix would be: application/views/scripts/.
So, your file should go here: application/views/scripts/form/_form.phtml
I’m not sure what path it would end up trying to load; a speculation without testing would probably be that it would be trying to load this: application/views/scripts/[application-path]/form/_form.phtml. As you can see, this path would not exist and would never work.
Your right Sir Henry. When I put my viewscript decorator to that path, it worked. =D
Sorry I am just new to zend and I am not really aware of this.
By the way, how can I change the path for my decorator viewscript so that it would not use its default path prefix?
Thanks for this post – I finally was able to get my custom forms working.
However there is one error in your code above. In:
application/views/scripts/form/_form.phtml
$this->getElement(‘username’);
should be
$this->element->getElement(‘username’);
Same on the password field.
My code isn’t wrong, it depends on the use case of the view decorator.
I.e. if you are using it for a form or for a form field group (fieldset) then you should use your example.
Also, you can iterate through the elements using:
foreach ($this->element as $_element) {
// Here, you can set styling around your elements etc.
$elementToUse = $_element;
}
It all depends on the use case of the ViewHelper decorator.
I got errors when I left off the element-> part. With that addition, it works. Lucas was right in my case. Could this be because of some configuration issue? In any case,it works now. Thanks!
Excellent breakdown, thank you very much!
I’m having difficulty getting the actual form elements to display, however. For some reason, the following:
getElement('property_type')?>is being rendered in the view as follows:
< ?php=$this->getElement('property_type')?>Have turned Google upside-down but couldn’t find any references to this. It’s like it’s being escaped but I have no idea why.
Any idea?
Thanks,
Nao
Try:
< ?=$this->getElement('property_type');?>Which the correct short code syntax.
Hi,
You should be using :
< ?=getElement('property_type') ?>
I’m not a fan of PHP short tags but I know many are
Regards,
Ian
I agree that he is using short tags wrong. However, I would discourage using short tags.
Thanks Henry,
Apparently my comment got mangled.. I am no fan of short-tags in PHP. I positively discourage them and my developers’ IDEs are configured to prevent the use of them. Looks like my original comment was edited – possibly because the php opening tag was stripped.
Use this format:
getElement('property_type') ?>
OK.. mu comments keep getting mangled..
<?php echo