using zend form decorators

For programming and general questions on Zend Framework
gpradeepjain
Posts: 6
Joined: Mon Mar 08, 2010 5:55 am

using zend form decorators

Post by gpradeepjain » Mon Mar 08, 2010 5:58 am

Code: Select all

<div class="field50Pct">
 <div class="fieldItemLabel">
  <label for='First Name'>First Name:</label>
 </div>
 <div class="fieldItemValue">
  <input type="text" id="firstname" name="firstname" value="" />
 </div>
</div>

<div class="clear"></div>
I want the code to appear like this in source code . how do i write the same thing in zend using decorators ? $firstname = new

Code: Select all

Zend_Form_Element_Text('FirstName');
        $firstname->setLabel('FirstName')
                 ->setRequired(true)
                 ->addFilter('StripTags')
                 ->addFilter('StringTrim')
                 ->addErrorMessage('Error in First Name')
                 ->addValidator('NotEmpty');

alokin
Posts: 75
Joined: Thu Mar 04, 2010 9:04 pm

Re: using zend form decorators

Post by alokin » Mon Mar 08, 2010 2:02 pm

Maybe something like this:

Code: Select all

$yourElement->addDecorator('Label')
        ->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'fieldItemLabel'))
        ->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'fieldItemValue', 'openOnly'=>true, 'placement'=>'append'))
        ->addDecorator('ViewHelper')
        ->addDecorator('HtmlTag', array('tag'=>'div', 'closeOnly'=>true, 'placement'=>'append'))
        ->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'field50Pct'));

gpradeepjain
Posts: 6
Joined: Mon Mar 08, 2010 5:55 am

Re: using zend form decorators

Post by gpradeepjain » Tue Mar 09, 2010 6:52 am

i am new to zend!can you explain me the code to me .

i have few doubts like . how does it know tht it has to apply the style to label of the value field or entire field like that of 50pct .

alokin
Posts: 75
Joined: Thu Mar 04, 2010 9:04 pm

Re: using zend form decorators

Post by alokin » Tue Mar 09, 2010 11:09 am

HtmlTag is rendered around all previously rendered content, unless you supply 'placement' option, like in that third and fifth call of addDecorator() method.

So, here's how that example from my previous post works:
1. label is rendered first
2. div tag with class "fieldItemLabel" wraps around everything that is previously rendered (in this case only label)
3. then we must only append opened next div tag with "fieldItemValue" CSS class
4. viewHelper decorator is rendered, which is the actual textfield element
5. then we close opened div from step 3
6. And finally, we wrap div with "field50Pct" CSS class around everything that is previously rendered.

gpradeepjain
Posts: 6
Joined: Mon Mar 08, 2010 5:55 am

Re: using zend form decorators

Post by gpradeepjain » Tue Mar 09, 2010 11:51 am

wow that was a beautiful explanation .thanks u so much.1 last doubt i have regarding the 'placement'=>'append' .If i dont add the statement it will wrap all last displayed html entities with this tag rite . any other placement options ?

How do i add a blank div like

<div id ='new'>
</div>

i dont want any element in this bcos i want to display dynamic content in it .

Code: Select all

        ->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'fieldItemValue', 'openOnly'=>true, 'placement'=>'append'))

alokin
Posts: 75
Joined: Thu Mar 04, 2010 9:04 pm

Re: using zend form decorators

Post by alokin » Tue Mar 09, 2010 12:23 pm

Opposite of append is prepend, so that's the one more placement option. :)

If you want to add empty div, you only need to supply placement option, with value prepend or append, (and id attribute if you want div with id "new") as you don't want to wrap it around anything, for example:

Code: Select all

$yourElement->addDecorator('HtmlTag', array('tag'=>'div', 'id'=>'new', 'placement'=>'append'));

gpradeepjain
Posts: 6
Joined: Mon Mar 08, 2010 5:55 am

Re: using zend form decorators

Post by gpradeepjain » Wed Mar 10, 2010 5:38 am

How do i add decorators for whole form like

Code: Select all

<div id='fieldform'>
<form>
</form>
</div>

and decorator for a field set .


I have a search form where in the user enters the id and searches for the patient . i had written the code like below . when there is no decorators applied to the form it works. when i apply the decorators the table structure do not get displayed. how to solve this problem.

Code: Select all

<?php echo $this->form ;?>
<p></p>
<br/>
<br/>
<br/>
<?php $patients = $this->patients; ?>
<?php if($patients) { ?>
<table border='1'>
<tr>
    <th>Patient Id</th>
    <th>Name</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
</tr>
<tr>
    <td><?php echo $patients['Patient_id'] ;?></td>
    <td><?php echo $patients['FirstName'] ?> <?php echo $patients['LastName'];?></td>
    <td><a href="<?php echo $this->url(array('controller'=>'index', 
            'action'=>'view', 'patient_id'=>$patients['Patient_id']));?>">View</a></td>
    <td><a href="<?php echo $this->url(array('controller'=>'index', 
            'action'=>'add-appointment', 'patient_id'=>$patients['Patient_id']));?>">Add Appointment</a></td>
</tr>
</table>
<?php } ?>

gpradeepjain
Posts: 6
Joined: Mon Mar 08, 2010 5:55 am

Re: using zend form decorators

Post by gpradeepjain » Thu Mar 18, 2010 11:31 am

no solution?

jeswin
Posts: 2
Joined: Wed Aug 08, 2012 7:28 am

Re: using zend form decorators

Post by jeswin » Wed Aug 22, 2012 6:47 am

gpradeepjain wrote:i am new to zend!can you explain me the code to me .

i have few doubts like . how does it know tht it has to apply the style to label of the value field or entire field like that of 50pct .

pls give the set decorator code also.....

hari6964
Posts: 1
Joined: Fri May 24, 2013 7:48 am

Re: using zend form decorators

Post by hari6964 » Fri May 24, 2013 7:51 am

gpradeepjain
if i use

$ele->addDecorator('ViewHelper')->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'fieldItemValue', 'placement'=>'append'));

Then default dd dl or form element decorators gets overrride.
I want default form element decorators and want to add empty div as form element aafter actuall form element.

Post Reply