symfony2 form field label as an array - forms

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

Related

Disable field in the crud

I need to update a record, but I want to display some fields as disabled for editing. I don't want to let the user edit a certain field, but I want to display them
If I understand your problem correctly then you may want to try something like this.
CRUD::addField([
'name' => 'nameOfYourField',
'label' => 'nameOFLabel',
'type' => 'number',
'attributes' => [
'readonly' => 'readonly',
],
]);
You can also use 'disabled' => 'disabled', but it depends what you want exatly to achieve with the values.
Check it out here

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 what is the best way to make this kind of form

i am making a shopping website and i got a probleme with some form. it's about the size part. I am gonna show some screenshots it's gonna be easier to understand :
what type of field is this kind of form ?
and then i would like the get this to show a list for client
i dont ask you to do the job for me but if you had some stuff (like a tutorial or some documentation about this kind of form) or some cluee i would really appriciate
Thx !
I'm not sure, but I think you are looking for Symfony's Form Types Reference.
In this case, you are probably talking about a choice field. A choice field can be rendered as a set of checkboxes / radio buttons (as in the first image), or as a <select> element (dropdown list), as in the second image, depending on wether the expanded option is set to true or false.
Radio buttons:
$builder->add('size', 'choice', array(
'choices' => array('s' => 'S', 'm' => 'M', 'l' => 'L', 'xl' => 'XL'),
'expanded' => true,
));
Dropdown list:
$builder->add('size', 'choice', array(
'choices' => array('s' => 'S', 'm' => 'M', 'l' => 'L', 'xl' => 'XL'),
'expanded' => false,
));
The default value of expanded is false, so if you don't specify it, the field will be rendered as a dropdown list.
If you have all available sizes stored in a table in your database, you might want to look at the entity field type as well. The entity type basically extends the choice type, but retrieves the available choices from the database:
$builder->add('size', 'choice', array(
'class' => 'MyWebshopBundle:Size',
'property' => 'name',
'expanded' => false,
));
This will fetch all Size entities from the database, and uses their name property in the dropdown list.
It's a choice type field
$form = $this->createFormBuilder($data)
->add('size', 'choice',
array('choices' => array(
1 => 'X',
2 => 'XL',
...
),
'data' =>'selectionnez la taille'
))
->getForm();
The option multiple can switch between radio button (false) and checkboxes ( true)
If you prefer a list like this you have to set the expanded attribute to false, true gives checkboxes or radios.
http://symfony.com/doc/current/reference/forms/types/choice.html

CakePHP FormHelper multiple select does not remember settings

In my form I have a input field with multiple checkboxes.
This works as expected.
When for some reason (eg an obligated field in the form is empty upon submit) the form after submit is not posted, all other fields maintain there input except the field below, all checkboxes become unchecked.
What can I do to make this field remember it's settings?
$options = array(
'1' => 'one',
'2' => 'two',
'3' => 'three'
);
echo $this->Form->input('checkboxes',array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => $options,
'default' => array(1,2,3)
));
FormHelper uses the $this->request->data to fill the fields. You need to keep (or fill) your data in request data array and the cakephp will maintain your data.

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

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.