Applying Class to Customer Zend Decorator - zend-framework

I found code that would change the standard dt and dd tags to table tags for a Zend_Form_Element. Here is the code I used:
$element->setDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
array('Label', array('tag' => 'td', 'class' => 'rightAlign')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
But this puts the class name 'rightAlign' on the label tag instead of the td. I can't seem to wrap my head around these custom decorators so can anyone tell me how to get the class name 'rightAlign' on the td surrounding the label?

Just add one more decorator
$element->setDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
'Label',
array(array('labelWrap' => 'HtmlTag'), array('tag' => 'td', 'class' => 'rightAlign')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));

$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
array('Label',array('requiredSuffix' => ' * ')),
array(array('labelWrap' => 'HtmlTag'), array('tag' => 'td', 'align' => 'right')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
will add the required suffix ;)

Related

Zend form button alignment

I have a Zend form with two buttons, Submit and Reset. See the below form code.
$submit = $this->CreateElement('submit','submit')
->setLabel('SUBMIT');
$submit->setDecorators(
array('ViewHelper',
'Description',
'Errors',
array(
array('data' => 'HtmlTag'),
array('tag' => 'td',
'colspan' => '2',
'align' => 'center')
),
array(
array('row' => 'HtmlTag'),
array('tag' => 'tr',
'closeOnly' => 'true')
)
)
);
$reset = $this->CreateElement('reset', 'reset')
->setLabel('RESET');
$reset->setDecorators(
array('ViewHelper',
'Description',
'Errors',
array(
array('data' => 'HtmlTag'),
array('tag' => 'td')
),
array(
array('row' => 'HtmlTag'),
array('tag' => 'tr',
'closeOnly' => 'true')
)
)
);
This is my form output image:
This reset and submit button is an ugly view. I don't like this view.
How do I set the reset and submit button in the same line?
Use valign="top" for both buttons.

How to add two addDisplayGroup in Zend Decorators

addDisplayGroup 1:
$this->addDisplayGroup(DATEFROM,ELEM_DATETO),
'Date', array('order' => 4,
'decorators' => array('FormElements',
array(array('openinnerdiv' => 'HtmlTag'),
array('tag' => 'div', 'id'=>'date_to_from','name'=>'date_to_from','class'=>'date_to_from')),
array(array('opendiv' => 'HtmlTag'),
array('tag' => 'div','id'=>'date' ))
),
)
);
addDisplayGroup 2:
$this->addDisplayGroup(array('AddBlock','Add','AddDate','ORDERID','COUNTRYCODE','DATEFROM','DATETO','AGE','GENDER','LIST','CAMERA'),
'queryblockfld_1',array('order' => 4,
'decorators' => array('FormElements',
array(array('openinnerdiv' => 'HtmlTag'),
array('tag' => 'div', 'id'=>'queryblockfld_1','name'=>'queryblockfld','class'=>'queryblockfld')),
array(array('opendiv' => 'HtmlTag'),
array('tag' => 'div','id'=>'queryblock' ))
),
)
);
I want to Add addDisplayGroup1 to addDisplayGroup 2. Can any one help me in this.?
After a long research in Web I found the answer for my own question:
$this->addDisplayGroup(array('DATEFROM','DATETO'),
'contact',array('legend' => 'Contact Information'));
$from_to = $this->getDisplayGroup('contact');
$from_to->setDecorators(array('FormElements',
array(array('openinnerdiv' => 'HtmlTag'),
array('tag' => 'div', 'id'=>'date_1','name'=>'date_1','class'=>'date_1','openOnly'=>true)),
array(array('opendiv6' => 'HtmlTag'),
array('tag' => 'div','id'=>'blockfld_1','class'=>'blockfld','openOnly'=>true)),
array(array('opendiv' => 'HtmlTag'),
array('tag' => 'div','id'=>'block','openOnly'=>true)),
));
$this->addDisplayGroup(array('AddBlock','Add','AddDate','NUMBERPLATE','COUNTRYCODE','NODE','CAMERA','NODELIST','NODECAMERA'),
'pass',array('legend' => 'Password'));
$pass = $this->getDisplayGroup('pass');
$pass->setDecorators(array(
'FormElements',
array('HtmlTag',array('tag'=>'div','closeOnly'=>true))
));
I just modified for the answer so some small mistakes will be there kindly correct it.

Zend File Upload and Element Decorators

I have the problem, that the following Zend Form throws an error.
The problem is the "file"-element and using setElementDecorators.
class Products_AddForm extends Zend_Form
{
function init() {
// other form elements...
$uploadElement = new Zend_Form_Element_File('Excel');
$uploadElement->setLabel('Excel');
$this->addElement($uploadElement);
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'th')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
}
}
This throws an error.
(Warning: Exception caught by form: No file decorator found... unable to render file element Stack Trace: #0 )
Adding $uploadElement->addDecorator('File'); at the end after the SetElementDecorators will work, but this will give me the file element twice!
Can anybody help, please?
TIA
Matt
The File element requires it's own decorator - Zend_Form_Decorator_File.
$this->setElementDecorators(array(
'File',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'th')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
[edit]
Have just noticed that you are also using other form elements.
After your original code, add:
$this->getElement('Excel')->setDecorators(
array(
'File',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'th')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
)
);
That way, ViewHelper is added to all other elements, and for your File element File is used instead.

How to set Decorators for Input type file Elements?

I have the following piece of code,
Under myClass , i have set decorators variable,
public $testDecorators = array(
'ViewHelper',
'Errors',
array('Description', array('escape' => false, 'tag' => '', 'placement' => 'append')),
array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => 'itemR')),
array('Label', array('tag' => 'div', 'class' => 'itemL')
),
array('HtmlTag', array('tag' => 'div', 'class' => 'itemcontent'))
);
Under CreateForm function,
....
$cover_image = new Zend_Form_Element_File('cover_test', array(
'label' => 'Cover Test:',
'value' => '',
'class' => 'test',
'tabindex' => '5',
'required' => false,
'filters' => array('StringTrim'),
'decorators' => $this->testDecorators,
));
....
When i use this decorators, nothing is displaying in my form, if commented that 'decorators' => $this->testDecorators, form is coming fine with default dd tag, Kindly help me
The file element must include the 'File' decorator, usually in place of the ViewHelper decorator. So try this instead:
public $testDecorators = array(
'File',
'Errors',
array('Description', array('escape' => false, 'tag' => '', 'placement' => 'append')),
array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => 'itemR')),
array('Label', array('tag' => 'div', 'class' => 'itemL'),
array('HtmlTag', array('tag' => 'div', 'class' => 'itemcontent'))
);
Have you looked at what $cover_image->getDecorators() shows?
Also, is this one in the middle correct:
array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => 'itemR')),
should it not be:
array('HtmlTag', array('tag' => 'div', 'class' => 'itemR')),
as the last one is?

Zend Framework: What is the right way of setting default decorators for Zend_Form elements

i currently set my default decorators for my Zend_Form using a class extending from Zend_Form ...
class Application_Form_Abstract extends Zend_Form {
...
function loadDefaultDecorators() {
if ($this->loadDefaultDecoratorsIsDisabled()) {
return $this;
}
// ... for elements
$decorators = $this->_elementDecorators;
if (empty($decorators)) {
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'escape' => false)),
'Label',
array('HtmlTag', array('tag' => 'p'))
));
but i soon realize that that way, i cannot define specific element decorators like
$this->addElement('textarea', 'bio', array(
'decorators' => array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'escape' => false)),
'Label',
array('HtmlTag', array('tag' => 'p')),
new Application_Form_Decorator_WmdPreview,
)
));
as they will be over-written by my custom loadDefaultDecorators() function. i wonder if there is any way i can set default decorators for element only if they have no set decorators
You can disable the default decorator for 'bio' element by adding a call to setDisableLoadDefaultDecorators()
$this->addElement('textarea', 'bio', array(
'disableLoadDefaultDecorators' => true,
'decorators' => array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'escape' => false)),
'Label',
array('HtmlTag', array('tag' => 'p')),
new Application_Form_Decorator_WmdPreview,
)
));
Also to you save you a headache, displaygroups cannot have the same name as any element that they contain