How do I achieve the following with form decorators for form elements:
<dt>
<ul>
<li>The errors</li>
<li>The errors</li>
</ul>
<label>The label</label>
</dt>
<dd>
<input type="text" value="The input field">
</dd>
In other words, in stead of Errors appended after the input field, I want them prepended before the Label. I do however want to keep the <dt> and <dd> tags as illustrated above.
Alright, I found out how to do it. Gradually the decorators are starting to make sense to me:
$decorators = array(
'Label',
array( 'Errors', array( 'placement' => 'prepend' ) ),
array( array( 'dt' => 'HtmlTag' ), array( 'tag' => 'dt' ) ),
array( array( 'ddOpen' => 'HtmlTag' ), array( 'tag' => 'dd', 'openOnly' => true, 'placement' => 'append' ) ),
array( 'ViewHelper' ),
array( array( 'ddClose' => 'HtmlTag' ), array( 'tag' => 'dd', 'closeOnly' => true, 'placement' => 'append' ) )
);
What this does is the following:
First render the Label
Then prepend (default = append) the Errors
Wrap (default) all previous content in a HtmlTag (dt)
Next, append (default = wrap) a opening HtmlTag (dd)
Then append (default) the ViewHelper
Next, append (default = wrap) a closing HtmlTag (dd)
Then set the decorators:
// be sure to only set them, after you have added the relevant elements to the form
$this->setElementDecorators( $decorators );
PS:
Be aware though that my particular example produces invaliid html. ;-) I only found out later that <ul> elements are not allowed in <dt> elements with DOCTYPE HTML 4.01 strict
In your form class, try this:
$this->setElementDecorators(array(
'Errors',
'ViewHelper',
'Label',
));
Related
I would like to know why was max_length in form type deprecated?
And how to achieve the desired effect the cleanest way now ?
See related issue on Github. This option only add html attribute to textarea. You can manually add it via attributes:
$builder->add('field', 'textarea', array(
'attr' => array('maxlength' => 255),
));
You can add it in builder:
$builder->add('field', 'textarea', array(
'attr' => array('maxlength' => 255),
));
or in twig:
{{ form_widget(form.field, {'attr': {'maxlength': 500}}) }}
IMPORTANT! Attribute value must be int, string will dont work:
$builder->add('field', 'textarea', array(
'attr' => array('maxlength' => '255'),
));
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 need to set a decoration for to print:
<input type="radio" id="option1" name="option" value="foo">
<label for="option1"></label>
<label>Option 1</label>
using Zend_Form_Element_Radio.
I've tried:
$pass = new Zend_Form_Element_Radio('options');
$pass->setLabel('Bloquear:')
->setRequired(true)
->addMultiOptions(array(
'option1' => '<label for="options-1"></label><label>Option 1</label>',
'option2' => '<label for="options-2"></label><label>Option 2</label>'));
return $pass;
but it prints the labels like a not HTML. Suggestions?
you have to define custom decorator as below example to manage with <ul> and <li>
it would be easy to apply css on bases of ul and li. take look on that
$this->addElement('radio', 'Bloquear', array(
'decorators' => array(
'ViewHelper',
array(array('AddTheLi' => 'HtmlTag'), array('tag' => 'li')),
array(array('AddTheUl' => 'HtmlTag'), array('tag' => 'ul')),
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
// specifying the "div" tag to wrap your <label> elements is not strictly
// necessary, but it produces valid XHTML if your form elements are wrapped
// in block-level tags like "<li>" (see next comment)
array('Label', array('tag' => 'div')),
// uncomment the following if all of your form elements are wrapped in "<li>"
//array('HtmlTag', array('tag' => 'li')),
),
'disableLoadDefaultDecorators' => true,
'label' => 'Bloquear',
'separator' => '</li><li>',
'attribs' => array(
'options' => array(
'foo' => 'Option 1',
'bar' => 'Option 2',
'baz' => 'Option 3'
),
),
));
please let me know if i can assist you more.
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
))
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)