How to set Decorators for Input type file Elements? - zend-framework

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?

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.

Applying Class to Customer Zend Decorator

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 ;)

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

Table Decorators on Zend Framework Form

i created a form that it decorates as table form
its my code for decorates
$this->setElementDecorators(array(
'ViewHelper',
'Errors'
array(array('data'=>'HtmlTag'),
array('tag'=>'td','class'=>'element')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr')),
));
$this->setDecorators(array(
'FormElements',
array('HtmlTag',array('tag'=>'table')),
'Form'
));
it works correctly,
now i wana errors message decorates too
what do i change my code?
Here is a rather complex way of doing it. I have added classes to the decorators too so you can style them unlike your example.
// To be assigned at the beginning of your form class.
public $elementDecorators = array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'col2')),
array('Label', array('tag' => 'td','class'=>'taR')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr','class' => 'rowA')),
);
$this->addElement('ValidationTextBox', 'name', array(
'decorators' => $this->elementDecorators,
'validators' => array(
array('regex', false,'/^[a-zA-Z ]+$/')
),
'label' => $this->translator->translate ( 'Name' ) . ' : ',
'required' => true,
'trim' => true,
'propercase' => true,
'regExp' => '[a-zA-Z ]+',
'invalidMessage' => $this->translator->translate ( 'Name - Must be alpha numeric.' )
)
);
If you want to show all erros grouped in one place you should remove the Error decorator from each element and then add to you form the formErrors decorator. Here is an example from How to remove Zend Form error messages?
$form->setDecorators(array(
'FormElements',
new Zend_Form_Decorator_FormErrors(array
(
'ignoreSubForms' => true,
'markupElementLabelEnd' => '</b>',
'markupElementLabelStart' => '<b>',
'markupListEnd' => '</div>',
'markupListItemEnd' => '</span>',
'markupListItemStart' => '<span>',
'markupListStart' => '<div id="Form_Errors">'
)
),
'Form'
));