Zend Framework forms, decorators and validation: should I go back to plain HTML?

Answer

I too find the default decorators to be a massive pain. I understand why they are the way they are, but I think the 'inconvenience factor' has been grossly underestimated.

Anyway, I would recommend using ViewScripts for your forms. Note that these are not the same as Views -- instead, ViewScripts are referenced explicitly in your Form class, act as a "sub-view" of sorts and allow you to control the layout of each and every element. Examples how to use ViewScripts have been somewhat hard to find in the past, but I'll try to take a stab at providing something useful.

First, override loadDefaultDecorators in your form class:

publicfunction loadDefaultDecorators(){
     $this->setDecorators(
         array(
             array('ViewScript', 
                 array('viewScript'=>'foo/bar.phtml'))));}

This will reference a ViewScript named bar.phtml located in /views/scripts/foo. Note the case-sensitive differences in "ViewScript" and "viewScript" above.

Next, you'll have to tweak the decorators applied to each element to ensure it displays but without the annoying dt/dd wrappers. For instance:

$baz =newZend_Form_Element_Text('bazInput');
$baz->setDecorators(array('ViewHelper','Errors'));

Finally, you'll need to build your ViewScript, such as:

<form method="post" action="<?php echo $this-element->getAction()?>">
    <table><tr><td><labelfor="bazInput">Baz:</label></td><td><?php echo $this->element->bazInput ?></td></tr></table><inputtype="submit"value="Submit Form"/></form>

Obviously this is a very simple example, but it illustrates how to reference the form elements and form action.

Then in your View, simply reference and output your form as usual. This way you can have much finer control over your form layouts -- to include easily adding Javascript.

I believe this approach resolves both your requirements: you can build forms in plain HTML and still take advantage of the Zend Form validation mechanism.

All zend-framework Questions

Ask your interview questions on zend-framework

Write Your comment or Questions if you want the answers on zend-framework from zend-framework Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---