How do I allow html tags in label for Zend form element using addElement()? - zend-framework

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.

Related

How can I Validate All Children of a Form Collection?

I have spent a long time without making progress on what seems to be a simple problem. I need to allow the user to add up to three references to a form. Each reference will have a name and phone number (two text inputs). I would like to do some simple validation on each one. I have created my ReferenceType and nested it into the main form with the following (note, I created the PhoneNumber validator and added the Null() just for testing):
$builder->add('references', 'collection', array(
'type' => new ReferenceType(),
'by_reference' => false,
'allow_add' => true,
'label' => false,
'required' => false,
'attr' => array('class' => 'reference'),
'options' => array(
'required' => false,
'label' => false,
),
'cascade_validation' => true,
'constraints' => array(
new Null(),
new PhoneNumber(),
)
));
ReferenceType:
$builder->add('name', 'text', array(
'label' => 'Reference',
'required' => false,
'cascade_validation' => true,
'attr' => array(
'placeholder' => 'Name'
)
));
$builder->add('phone', 'text', array(
'label' => false,
'required' => false,
'attr' => array(
'placeholder' => 'Phone Number'
),
'cascade_validation' => true,
'constraints' => array(
new Length(array('max' => 14)),
new PhoneNumber(),
)
));
All of that is quite simple and straight forward. However, I have been unable to get the form to throw an error for any reason related to those references. I use Propel for my ORM and have a lot of validation which works correctly on the main form. These references also display and function correctly except for validation. They are all mapped to one column (references) and I have tried Propel's type="array" as well as defining my own getter and setter which serializes the array of items. The type=array doesn't work at all with the collection, and serializing it seems to work ok.
I have searched SO and symfony.com docs for an answer but have found nothing that would actually cause either name or phone to throw an error. I have tried adding validation to my validation.yml file without success (3 chars only for testing):
references:
- Valid: ~
- Collection:
fields:
name:
- Length:
max: 3
maxMessage: Please limit your reference's name to 3 characters or less.
I'm probably missing something very obvious here, but I can't seem to grasp what. I have also dug through the Profiler and everything appears correct in there as well.
Could any provide hints on things to look for? It seems that creating a new database just for references is a little overkill (they don't need to be searchable or anything). Propel has some information on collections, but it didn't seem to make a difference. Or am I entirely missing the idea here and perhaps I should implement the fields an entirely different way?
Symfony 2.6
Propel 2

zend addElements form onchange select

I try to add dinamically a form input Elemnt on onchange of select element.
I have this code:
$this->addElement('select', 'nationality', array(
'multiOptions' => array('CH' => 'Choose','IT' => 'Italy', 'other' => 'Other'),
'required' => true,
'label' => 'Nation',
'filters' => array('StringTrim'),
'onChange' => '???'
));
I try to paste my new element instead "???"
$this->addElement('select', 'nationality', array(
'multiOptions' => array('CH' => 'Choose','IT' => 'Italy', 'other' => 'Other'),
'required' => true,
'label' => 'Nation',
'filters' => array('StringTrim'),
'onChange' => '$this->addElement(
'text', 'codice_fiscale', array(
'required' => true,
'label' => 'Codice fiscale',
'required' => true
))'
));
I know is not correct, but I dont' found any documentation about it.
How can I add an element onchange select value?
Thanks in advance
I would approach the problem in a different way, if it's possible for you:
javascript functions to call on the onChange. It would be syntax correct (and, as you said, what you wrote is not correct, it's going to be printed out entirely on the onChange attribute, not interpreted through php).
so, i would write
'onChange' => 'addElement()'
and then declare the javascript function which handles new elements. Or, you can prepare the elements before (if you have a fixed number), and show / hide them. It may be convenient if you don't have much elements, but if they're too many, just add through javascript the needed one.
PseudoCode Javascript
function addElement()
{
// add your element here, or show / hide it
}
As far as I saw from my experience and similar cases
(Zend_form_element_select onchange in zend framework,
Zend Form: onchange select load another view content)
you won't escape from Javascript if you want to handle the "onChange"

symfony2 form field label as an array

I'd like to have 3 separate texts for each field in my form as a label. They are separate, because they need to be styled differently. I tried this:
$builder->add('total_sales', 'text', array(
'label' => array('num' => '1', 'descr' => 'Total sales', 'category' => 'A'),
'required' => false,
'attr' => array(
'class' => 'field numeric_field',
'maxlength' => 10,
)));
Obviously the above don't work; it will display 'Array' in place of label.
How can I achieve desired effect?
first you'll need to create a custom form type that extends the text type, the reason for this is so you don't mess up other text types you might have elsewhere. After doing that you'll need to style it using a form_div_layout. you can see the details here:
http://symfony.com/doc/current/cookbook/form/form_customization.html

how to add decoratives in select box in zend framework Form

I am new to zend I want to add decoratives,size in my select box
example like this
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:103px'),
'multiOptions' => $list,
'decorators'=>Array(
'ViewHelper','Errors'
),
this is my code
$companyName = new Zend_Form_Element_Select('companyName');
$this->addElement($companyName);
Your question isn't very clear, but you can supply an array of options as the second parameter to the form element:
$companyName = new Zend_Form_Element_Select('companyName', array(
'required' => true,
'filters' => array('StringTrim')
));
$this->addElement($companyName);
This tutorial will help you
http://www.codexperience.co.za/post/creating-good-looking-zend-forms
Its all about styling your zend forms using the traditional way whilst retaining the awesomeness that Zend_Form comes with

onblur function call in zend framework add element

I am new to fend framework. I have a input box in html with on blur property which call a ajax function code is given below:
input name="companyCode" type="text" id="companyCode" style="width:220px"
onblur="getOrganizationode(this.value);"
and a ajax script tags.
Basically i want my input box in Zend form with onblur function call in Add element function like this
addElement( 'text', 'companyCode' , array(
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:220px'),
so please help me
You can just add the call to the attributes:
addElement('text', 'companyCode', array(
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:220px'),
'onBlur' => 'getOrganizationode(this.value);'
);