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
))
Related
I am brand new to Zend and I've been given a project to make adjustments on. I'd like to add html to the labels for my form elements but I can't seem to get it right.
Here's what I have:
$this->addElement('text', 'school_name', array(
'filters' => array('StringTrim'),
'validators' => array(
array('StringLength', false, array(0, 150)),
),
'required' => true,
'label' => 'Name* :<img src="picture.png">,
'size' => '90',
));
As is, of course, the <img src="picture.png"> text gets escaped and the whole string is displayed.
I've read that I need to use 'escape' => false in some capacity but I can't figure out where/how to use it in my specific case.
Any help would be great. Thanks!
After calling addElement fetch the label's decorator and change the escape setting:
$form->getElement('school_name')->getDecorator('label')->setOption('escape', false);
If you use this type of label a lot, you should consider writing a custom decorator.
You can also use the disable_html_escape in 'label_options' when adding an element to the form:
$this->add(array(
....
'options' => array(
'label' => '<span class="required">Name</span>,
'label_options' => array(
'disable_html_escape' => true,
)
),
...
));
Credit to Théo Bouveret's post 'Button content in ZF2 forms' for the answer.
I try to put some HTML link behind input text and I try to do it's somthing like this:
$aElements[$iKey] = $oName = new Zend_Form_Element_Text($aValue['newsletter_question_answer_id']);
$oName->addDecorator('HtmlTag', array(
'tag' => 'a',
'href'=>'http://some_url.html',
'placement' => Zend_Form_Decorator_Abstract::APPEND
));
and my question is how can I put somthing between <a> and </a> ?
Best Regards
If You don't want to write Your own decorator You have to use callback:
$element->addDecorator('Callback', array(
'callback' => function($content, $element, $options) {
Zend_Debug::dump($content, 'content'); //elements decorated so far
Zend_Debug::dump($element, 'element'); //current element
Zend_Debug::dump($options, 'options'); //other options
return "{$options['label']}";
},
'option' => 'value', //everything but 'callback' and 'placement' gets
//passed to callback as option
'href' => 'http://example.com',
'label' => 'Link!',
'placement' => Zend_Form_Decorator_Abstract::APPEND
));
Ofcourse it's php5.3 style callback, but You can use oldstyle too.
I have a custom validator that checks all the values in a subform to make sure that they make sense in relation to each other. In the event that this validator fails, I'd like to have an error decorator at the top of the subform to display the error message. Is this possible?
I've already set up the decorators like so:
protected $_decorators = array(
array(
'decorator' => 'FormElements',
'options' => array()
),
array(
'decorator' => 'HtmlTag',
'options' => array(
'tag' => 'ul',
'class' => 'test'
)
),
);
And it seems like I should be able to add
array(
'decorator' => 'Errors',
'options' => array(
'tag' => 'ul',
'class' => 'errors',
'placement' => 'prepend',
)
),
but that causes Zend to fail with the error "htmlspecialchars() expects parameter 1 to be string, array given". What am I doing wrong then? Thanks!
I believe nothing is wrong in your code, just ZF doesn't handle the Errors decorator within Zend_Form_SubForm properly. I hope they will fix this soon.
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
In my form i have this code;
// Add the submit button
$element = $this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Add new material'
));
$element->removeDecorator('label');
However the form still renders with the label element between the tags.
What am i doing wrong?
This worked for me:
$this->addElements(array(
new Zend_Form_Element_Submit('submit', array(
'label' => 'Save'
))
));
$element = $this->getElement('submit');
$element->removeDecorator('DtDdWrapper');
I did print_r($element); to find out what decorators exist for $element.
The function addElement returns a reference to the current form not to the last added element.
You could try this:
$form = new Zend_Form();
$form->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Add new material'
));
$element = $form->getElement('submit');
$element->removeDecorator('label');
I think the argument to removeDecorator is case-sensitive. I.e., it should be "Label" # note the uppercase 'L'.
To overcome this nuisance I'm defining manually the decorators for my element...
$details->addElement('text', 'in_year', array(
'decorators'=>array(
'ViewHelper',
array('HtmlTag', array('tag' => 'span')),
)
));
You can of course define your own tags. In this example I only initialise the "ViewHelper" decorator. If I want to initialise the "Label" decorator I would do:
$details->addElement('text', 'in_year', array(
'decorators'=>array(
'ViewHelper',
'Label',
array('HtmlTag', array('tag' => 'span')),
),
'attribs' => array('class' => 'required validate-digits')
));
I hope this makes sense... :o)