How to keep your form with its exact layout By using the Zend Form



Today we will learn how to keep our layout exact like our project main design.
We know that if we use Zend form we find some flexibility and advantage for using the component:
1. You can filter and validate the input field.

2. You can group the element or can make the subform.

3. And also you can escape the data prior insert into database so the form filed will be safe from all spam

But the bitter experience for most of the developers is that it is tedious to preserve the actual markup because Zend form render the from element <dl><dt>. It put form level in <dl> , Input field in<dd> and total element in<dt>

 

Now we will make a form preserving our actual layout.

Step-1: Firstly we need to stop the dafault decorator which is

<dt>,

<dl>,

<dd>. 

$this->setDisableLoadDefaultDecorators(true);

Here $this refer to the Zend_Form object.
Now we will call the layout with actual form in where Zend_Form Classes are implemented.

$this->setDecorators(array(
array(‘ViewScript’, array(‘viewScript’ => ‘form/_form.phtml’)),
‘Form’
));

form/_form.phtml it refers that the _form.phtml file is exist in the [Your Project Destination]\application\views\scripts\form.

Now Create an Element like this:

$this->addElement(‘text’, ‘username’, array(
‘decorators’ => array(
‘ViewHelper’
),
));

And go to the [Your Project Destination]\application\views\scripts\form\_form.phtml

Embed this code

< ?php echo $this->element->getElement(‘username’); ?>

And you will see your form in the exact layout without any tension or the interruption of

<dt>
<dd>
<dl>
,

tag. 

And definitely it will save your time and help you to get the project development satisfaction.

Any question or suggestion regarding this will nicely be accepted. Comment option is open for all.

Be happy with your development time.


Leave a Reply