I have some problem to create "three columns table" form in Zend Framework:
I already have Zend Form decorated by tow columns table:
Table have two column, first one is for label and second one is for Zend_Form_Element, that works well, but,
I want to add third column and put there small image - question mark, where I will setup javascript.
How to set decoration for that?
Current decoration for two columns table is:
<?php
class Application_Form_Login extends Zend_Form {
public function init() {
// create decoration for form's elements
$elementDecoration = array(
'ViewHelper',
'Description',
'Errors',
array(array('data'=>'HtmlTag'), array('tag' => 'td', 'valign' => 'TOP')),
array('Label', array('tag' => 'td')),
array('Errors'),
array(array('row'=>'HtmlTag'),array('tag'=>'tr'))
);
$buttonDecoration = array(
'ViewHelper',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array(array('label' => 'HtmlTag'), array('tag' => 'td', 'placement' => 'prepend')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
);
$formDecoration = array(
'FormElements',
array(array('data'=>'HtmlTag'), array('tag'=>'table', 'class'=>'forms')),
'Form'
);
// create form elements
$username = new Zend_Form_Element_Text("username");
$username->setLabel('Username: ')
->setDecorators($elementDecoration);
$password = new Zend_Form_Element_Password("password");
$password->setLabel('Password: ')
->setDecorators($elementDecoration);
$submit = new Zend_Form_Element_Submit('Login');
$submit->setLabel('LOGIN')
->setDecorators($buttonDecoration);
$this->setDecorators($formDecoration);
// set created form elements
$this->setAction('')
->setMethod('post')
->addElement($username)
->addElement($password)
->addElement($submit);
}
}
It depends on if you only require to add <td class="fieldTip"></td>, or if you need to add things inside the <td></td>.
In the first case you just need to add a simple HtmlTag decorator right after the Label decorator :
array('HtmlTag', array('tag'=>'td','class'=>'fieldTip','placement'=> 'APPEND'))
If you want to put some sort of description of the field you should play with the Description decorator, and the $element->setDescription() method, and do some css/js after, to display it as a tooltip.
EDIT
I just answered an other question about simple custom decorators, you'll find what you need in the example i give there Zend Form Element with Javascript - Decorator, View Helper or View Script?. Just replace the <script> part with whatever you need.
Related
I am rendering a very simple form with a table format. I first add the elements, to later set their basic decorators with the following:
$this->setElementDecorators(array(
'Viewhelper',
array(array('data'=>'HtmlTag'),array('tag'=>'td')),
'Label',
array(array('labelCell'=>'HtmlTag'),array('tag'=>'td', 'align'=>'right')),
array(array('row'=>'HtmlTag'), array('tag'=>'tr'))
));
Afterwards, I manipulate whatever group of elements (as desired) to set up different looks, like: group elements in a single row. To do this last one, the following is carried out:
$this->getElement($elementName)->setDecorators(array(
'Viewhelper',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'colspan' => $colspan)),
'Label',
array(array('labelCell' => 'HtmlTag'), array('tag' => 'td', 'align' => 'right')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
As you see, I have to setDecorators() adding all of them (the default ones) again, so I can change the 'data' decorator and add the attribute "colspan".
My question would be: is it possible to access and change a single decorator without having to set all of the previous decorators the element had?
you should be able to call:
$viewHelperDecorator = $this->getElement($elementName)->getDecorator('ViewHelper');
Then, this is like any other decorator (it is a decorator abstract) - so you can call
$viewHelperDecorator->setOption();
and set the change you'd like.
I'm using DisplayGroup to render a couple of elements in a group. Now what I want to do, is render this group in a single table row. But it turns out, that Decorators on DisplayGroups do NOT work the same way they do on simple elements. An example:
$content = $this->getDisplayGroup('group');
$content->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'table', 'class' => 'element')),
));
This just puts
<table></table> at the end of my DisplayGroup. I've also tried to alias them:
array(array('elementDiv' => 'HtmlTag'), array('tag' => 'table')),
'FormElements',
array(array('td' => 'HtmlTag'), array('tag' => 'table')),
Still puts them in one place, not both tried openOnly and closeOnly :
array(array('elementDiv' => 'HtmlTag'), array('tag' => 'table','openOnly'=>true)),
'FormElements',
array(array('td' => 'HtmlTag'), array('tag' => 'table','closeOnly'=>true)),
And it still puts in only the closing tag. If I change these to <td>, everything works fine, but not with <table> Am I missing something?
So, jah was right, and the table not being added was really a bug of source viewers.
$group->setDecorators(array(
'FormElements',
array('Description', array('escape' => false, 'tag' => false)),
array('HtmlTag', array('tag' => 'table', 'class' => 'element')),
));
This did the trick for me. Not really sure what the Description Decorator does, but the table renders as needed.
how can i go abt setting up default decorators for all my forms & form elements? currently in individual forms i do something like
// in init()
...
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p')),
'Label',
array('HtmlTag', array('tag' => 'p'))
));
$this->getElement('btnLogin')->removeDecorator('Label');
$this->setDecorators(array(
'FormElements',
array('Errors', array('placement' => 'PREPEND')),
'Form'
));
one part i see maybe harder is i want my submit buttons not to have a label.
For forms you can override Zend_Form class with your own and redefine loadDefaultDecorators method there. Then just inherit your forms from your form class. Zend_Form_Element also has loadDefaultDecorators method.
I'm aming to make use of the ZendX_JQuery dialogContainer view helper, in order to produce a modal window, were users can input specified information(for example send a message). I'm trying to use the dialogContainer view helper in this fashion.
First of, include the ZendX library in the applications library folder.
Secondly, include the following row in the initViewHelper method within the Bootstrap.php file
"$view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');"
third, adding the following conditional enabling of js in the layout.phtml
"<?php if($this->jQuery()->isEnabled()){
$this->jQuery()->setLocalPath($this->baseUrl()
.'/js/jquery/js/jquery-1.4.2.min.js')
->setUiLocalPath($this->baseUrl()
.'/js/jquery/js/jquery-ui-1.8.custom.min.js')
->addStylesheet($this->baseUrl()
.'/js/jquery/css/ui-lightness/jquery-ui-1.8.custom.css');
echo $this->jQuery();
}
?>"
fourth, creating my Application_Form_JQueryForm extending ZendX_JQuery_Form
"<?php
class Application_Form_JQueryForm extends ZendX_JQuery_Form
{
private $form;
public function init()
{
$this->form = $this->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . '/index/index')
->setMethod('post');
$this->form->setDecorators(array(
'FormElements',
'Form',
array ('DialogContainer', array(
'id' => 'tabContainer',
'style' => 'width: 600px;',
'title' => 'Send a private message to Kalle',
'JQueryParams' => array(
'tabPosition' => 'top',
),
)),
));
$topic = new Zend_Form_Element_Text('topic');
$topic->setValue('topic')
->setRequired(true)
->setValidators(array('validators' => array(
'validator' => 'StringLength',
'options' => array(1,15)
)))
->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array('HtmlTag', array('tag' => 'dl'))));
$textarea = new Zend_Form_Element_Textarea('textarea');
$textarea->setValue('post a comment')
->setAttribs(array(
'rows' => 4,
'cols' => 20
))
->setRequired(true)
->setValidators(array('validators' => array(
'validator' => 'StringLength',
'options' => array(1,15)
)))
->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array('HtmlTag', array('tag' => 'dl'))));
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Send your comment')
->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array('Description', array('escape' => false, 'tag' => 'span')),
array('HtmlTag', array('tag' => 'dl'))))
->setDescription('or Cancel');
$this->form->addElements(array($topic, $textarea, $submit));
}
}"
This form is then instanciated in the controllers action method, and called in the view.
And so to the problem of mine, no matter what i try, in order to for instance set, the width of the dialogContainer or any other parameter (color, css, height, so on and so forth), this being in the JQueryForm's setDecorator part for the form, i can't seem to get any change whatsoever in the resulting modal when called in the view, any help in the proper direction would be greatly appreciated
Thanks in advance, Kalle Johansson
A late answer for sure - but lots of views - so figured I would answer. The parameters you want to set for the modal should be set from the JQueryParams array. So - for example:
$this->form->setDecorators(array(
'FormElements',
'Form',
array ('DialogContainer', array(
'id' => 'tabContainer',
'style' => 'width: 600px;',
'title' => 'Send a private message to Kalle',
'JQueryParams' => array(
'tabPosition' => 'top',
'width' => '600',
'height' => '450'
),
)),
));
You can find these param options on the jQuery UI site.
LK
I have changed decorator:
private function _addErrorDecorator($form)
{
$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'
));
return $form;
}
But now i need to remove error messages under form fields. How do i make it?
Each element, subform and display group in your form has a decorator stack as well, so you will need to modify the stack for the elements you want to not display the error messages.
There's a lot of ways to do this:
$form->setElementDecorators(array(
'ViewHelper',
'HtmlTag',
'Label'
));
Is the way to go if you want to keep the default element decorator stack, but with the error decorator removed. You can also do it on an individual element basis:
$element->setDecorators(array(
'ViewHelper',
'HtmlTag',
'Label'
));
Or when you are adding the element:
$form->addElement($type, $name, array(
'decorators' => $decorators
))