How to set decorators in zend form? - php

I am using zend. I am trying to set decorators for the following div structure ,
<div class="fm">
<div class="fm_text">
<div class="p_details">Debit Weight:</div>
</div>
<div class="fm_text">
<div class="txtbox">
<input type="text" name="pname" class="txtcolor">
</div>
</div>
<div class="fm_text">
<div class="p_details">Credit Weight:</div>
</div>
<div class="fm_text">
<div class="txtbox">
<input type="text" name="pname" class="txtcolor">
</div>
</div>
</div>
<div class="fm">
......
</div>
It is difficult to set decorator for above div structure.I tried with following code ,
public $requirednewElementDecorators = array(
'ViewHelper',
'Errors',
array('Description', array('escape' => false, 'tag' => 'span', 'class' => '', 'placement' => 'prepend')),
array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => 'fm_text')),
array('Label', array('tag' => 'div', 'class' => 'p_details')
),
array('HtmlTag', array('tag' => 'div', 'class' => 'fm'))
);
I have to use zend form so that i can validate form effectively. I can use the HTML in template and continue the flow. but i dont want to do that.Kindly help me.

Two input-fields togehter? You can try it with a fieldset for the <div class="fm">...</div>. So, you must use the fieldset decorator. Why do you not want to use the <label class="p_details">DebitWeight</label> instead of <div class="p_details">Debit Weight:</div>?
This article is very helpful, maybe: http://devzone.zend.com/article/3450-Decorators-with-Zend_Form

Related

Zend Form Decorators for theme

I got a new theme for which i need to edit my decorators.
Most of it i got working except for the checkbox and radio.
<!-- Previously -->
<div class="form-group ">
<label for="mobileTheme" class="col-lg-2 control-label optional">Mobiel thema</label>
<div class="col-lg-5">
<div class="checkbox">
<input type="hidden" name="mobileTheme" value="0" />
<input type="checkbox" name="mobileTheme" id="mobileTheme" value="1" class=" " />
</div>
<span class="help-block">Redirect mobiele gebruikers naar een eigen domein met eigen thema</span>
</div>
</div>
<!-- After -->
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox" value="">
<i class="input-helper"></i>
Remember me
</label>
</div>
</div>
</div>
My decorator looks like this now:
'checkbox' => array(
'decorators' => array(
'ViewHelper',
array(array('input' => 'HtmlTag'), array('tag' => 'div', 'class' => 'checkbox')),
array('Errors', array('class' => 'help-inline')),
array('Description', array('tag' => 'span', 'class' => 'help-block')),
array('Label', array('class' => 'col-lg-2 control-label')),
array('HtmlTag', array('tag' => 'div', 'class' => 'col-lg-5')),
'ElementWrapper'
),
'options' => array(
'class' => '',
),
),
The issue i'm struggling with is that i don't know how to order the decorators right so that label is now within the col-*-* class
I got it working,
'checkbox' => array(
'decorators' => array(
'ViewHelper',
array('AdditionalElement', array('placement' => 'APPEND', 'tag' => 'i', 'class' => 'input-helper')),
array(array('input' => 'HtmlTag'), array('tag' => 'div', 'class' => 'checkbox')),
array('Errors', array('class' => 'help-inline')),
array('Description', array('tag' => 'span', 'class' => 'help-block')),
array('HtmlTag', array('tag' => 'div', 'class' => 'col-lg-5')),
array('Label', array('class' => 'col-lg-2 control-label')),
'ElementWrapper'
),
'options' => array(
'class' => '',
),
),
AdditionalElement is my own custom class for implementing any piece of html.

Zend_Form: how to remove multiple <legend> elements in fieldset (nested DIV)

I want to get something like this:
<div class="widgetbox">
<legend class="widgettitle">Widget Box</legend>
<div class="widgetcontent"> Content goes here... </div>
</div>
my code look like this:
$this->addDisplayGroup(
$fields,
'main',
array(
'legend' => $this->_tlabel.'group_main',
'decorators' => array(
'FormElements',
array(
array('widgetcontent'=>'HtmlTag'), array('tag'=>'div', 'class'=>'widgetcontent')
),
array('HtmlTag',array('tag' => 'div', 'class' => 'widgetbox')),
)
)
);
and all I can get is:
<div class="widgetbox">
<legend>Main info</legend>
<div class="widgetcontent">
<legend>Main info</legend>
</div>
</div>
As you can see I get double legend elements, but I want only one - first on, right after div.widgetbox.
Can you help me remove unneeded legend element from nested div?
Thanks!
You can certainly use an h4 instead, but not as a legend. Try:
$this->addDisplayGroup(
$fields,
'main',
array(
'description' => 'Widget title',
'decorators' => array(
'FormElements',
array(array('widgetcontent' => 'HtmlTag'), array('tag'=>'div', 'class'=>'widgetcontent')),
array('Description', array('tag' => 'h4', 'placement' => 'prepend', 'class' => 'widgettitle')),
array(array('widgetbox' => 'HtmlTag'), array('tag' => 'div', 'class' => 'widgetbox'))
)
)
);
This adds a description decorator and sets its tag as <h4>. I also aliased the widgetbox decorator to make it clearer how it all fits together. This gives me:
<div class="widgetbox">
<h4 class="widgettitle">Widget title</h4>
<div class="widgetcontent">...</div>
</div>
Like I said in my comment, since your code doesn't include a fieldset decorator, I don't see how what you've posted could include any legends at all, so it seems like there's something elsewhere in your app that's changing the decorators after this runs. If you're still getting legends, you need to try and figure out where they are being added.

How to replace label tags in Zend Form

The task is to replace label tags in the Zend Form by any others (i.e. div) using decorators or something
Now I have
<label for="field1" class="required">Field1</label>
<input type="text" name="field1" id="field1" value="" size="20">
What I want to have
<div>Field1</div>
<input type="text" name="field1" id="field1" value="" size="20">
Is it possible? How to do it?
Somethin like this
$this->setElementDecorators(array(
array('ViewHelper'),
array('Errors', array('tag' => 'div', 'class' => 'error')),
array('Label', array('tag' => 'span')),
array('HtmlTag', array('tag' => 'div', 'class' => 'label')),
));
For more reference visit this link
Do not know if that is possible with the label, I guess you would have to rewrite how the label elements are rendered in the form. I managed simply to to the same using the description tag:
<div class="input-group input-group-sm col-md-12">
<span class="input-group-addon">Name of the field</span>
<input id="name" class="form-control" type="text" name="name">
</div>
This is done by using a decorator like:
public $inputDecorators_md12 = array(
'ViewHelper',
array('Description', array('escape' => false, 'tag' => 'span', 'placement' => 'prepend', 'class'=>'input-group-addon')),
array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'input-group input-group-sm col-md-12'))
);
Note that you can define any type of tag that you want for the description.
so instead of
$field->setLabel('fieldlabel')
you can do:
$field->setDescription('fieldlabel')
->setDecorators($this->inputDecorators_md12);

How to decorate 'Errors' using standard decorators?

I have set the form decorators in this way:
<?php
$this->setElementDecorators(array(
'Label',
array(array('labelTd' => 'HtmlTag'), array('tag' => 'td', 'class' => 'name')),
array(array('elemTdOpen' => 'HtmlTag'), array('tag' => 'td', 'class' => 'form','openOnly' => true, 'placement' => 'append')),
'ViewHelper',
'Errors',
array(array('elemTdClose' => 'HtmlTag'), array('tag' => 'td', 'closeOnly' => true, 'placement' => 'append')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr', 'class' => 'question')),
));
$submit->setDecorators(array('ViewHelper',
array(array('data' => 'HtmlTag'), array('tag' =>'td', 'class'=> 'element')),
array(array('emptyrow' => 'HtmlTag'), array('tag' =>'td', 'class'=> 'element', 'placement' => 'PREPEND')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
$this->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'table', 'class' => 'simpleform')),
'Form'
));
It outputs a simple table
<table class="simpleform">
<tbody>
<tr class="question">
<td class="name">
<label class="required" for="email">Your email</label>
</td>
<td class="form">
<input type="text" value="asasd" id="email" name="email">
<ul class="errors">
<li>'asasd' is no valid email address in the basic format local-part#hostname
</li>
<li>Information not found
</li>
</ul>
</td>
</tr>
<tr>
<td class="element"></td>
<td class="element">
<input type="submit" value="Send" id="submit" name="submit">
</td>
</tr>
</tbody>
</table>
I would like to wrap ul.errors to TD and put it as the third cell.
Like that:
<tr class="question">
<td class="name">
<label class="required" for="email">Your email</label>
</td>
<td class="form">
<input type="text" value="asasd" id="email" name="email">
</td>
<td>
<ul class="errors">
<li>'asasd' is no valid email address in the basic format local-part#hostname
</li>
<li>Information not found
</li>
</ul>
</td>
</tr>
and.. how to do that? :)
I suggest you create your own Errors decorator which will do what you need.
You can for example set it up to output
</td>
<td>
<ul class="errors">
<li>'asasd' is no valid email address in the basic format local-part#hostname
</li>
<li>Information not found
</li>
</ul>
If you know the errors will always be part of a table within <td></td> tags.
In general, no framework is flexible enough to cover all scenarios and frameworks shouldn't be slowing you down in development. If you cannot do something using framework and you cannot extend the framework to satisfy your requirement, go around it and don't use it in this particular case. It's not said all the forms have to use Zend_Form :)

How to wrap Zend_Form error message in custom html?

I need to wrap zend form error messages in custom html.
<div class="cerror" id="ID-error">
<div class="ui-widget">
<div class="ui-state-error ui-corner-all" id="IDerror-msg">
%ZEND_FORM_ERROR_MESSAGE%
</div>
</div>
</div>
Now I get errors in format:
<ul>
<li>Error message</li>
</ul>
I need:
<div class="cerror" id="EMAIL-error">
<div class="ui-widget">
<div class="ui-state-error ui-corner-all" id="EMAIL-error-msg">
<ul>
<li>Error message</li>
</ul>
</div>
</div>
</div>
Thank you!
I have following code:
$element->clearDecorators();
$element->removeDecorator('DtDdWrapper');
$element->addDecorator('ViewHelper');
$element->addDecorator('Description', array('tag' => 'p', 'class' => 'description'));
$element->addDecorator('Label', array('tag' => null));
$element->addDecorator(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-line'));
How to wrap errors in 3 div tags? Thank!
$element->clearDecorators();
$element->addDecorator('Errors');
$element->addDecorator(array('div1' => 'HtmlTag'), array('tag' => 'div',
'class' => 'cerror', 'id' => 'EMAIL-error'));
$element->addDecorator(array('div2' => 'HtmlTag'), array('tag' => 'div',
'class' => 'ui-widget'));
$element->addDecorator(array('div3' => 'HtmlTag'), array('tag' => 'div',
'class' => 'ui-state-error ui-corner-all',
'id' => 'EMAIL-error-msg'));
$element->addDecorator('ViewHelper');
$element->addDecorator('Description', array('tag' => 'p', 'class' => 'description'));
$element->addDecorator('Label', array('tag' => null));
$element->addDecorator(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-line'));

Categories