Show labels for datetime form fields in Symfony2 - symfony

I'm using Symfony2.1 (beta). I can't find a way to display labels for datetime fields in forms.
In the reference there is no trace of a label property. How can it be?!

You can show the label of a field "myDate" for example like this:
<div>
{{ form_label(form.myDate, 'Choose a date: (this is the label sentence)') }}
</div>
(Documentation: http://symfony.com/doc/current/book/forms.html)
You can also personalize your form render: http://symfony.com/doc/current/cookbook/form/form_customization.html

Try this. I use it every time and works like a charm. Maybe docs bug?
$builder->add('creation_date', 'date', array(
'label' => 'Creation date',
));

I had same issue with Symfony 2.7
I fixed this way:
->add('issuedAt', 'date', array( 'label_render' => true,
'show_child_legend' => false,
'label' => 'Issued at date:',
...

Related

Sonata Admin datetime picker returns wrong time

I'm new in Symfony, so excuse my ignorance.
I try to edit date and time of some event using Sonata Admin and 'sonata_type_datetime_picker' field type.
But sonata_type_datetime_picker return wrong time. It gives time for an hour less.
Here's a filled form field screenshot (time is 10:00):
Here's dump of form field value (time is 09:00):
Here's form field options:
->add('datebegin', 'sonata_type_datetime_picker', array(
'format' => 'dd.MM.yyyy HH:mm',
'label' => 'Event begins at...',
'dp_use_current' => false,
'model_timezone' => 'Europe/Moscow'
))
I'd like to note that this error exists only on the server. On my local machine everything is ok.
What could it be?
Thanks a lot in advance!
I've got this problem too. I think that you need to upgrade the software on your server (packet: tzdata etc).
See: https://en.wikipedia.org/wiki/Moscow_Time
If this doesn't help you, you can use this hack:
'view_timezone' => 'Europe/Minsk'
See:
https://en.wikipedia.org/wiki/Time_zone
https://en.wikipedia.org/wiki/UTC%2B03:00
https://en.wikipedia.org/wiki/Minsk (UTC+3)
For example:
->add('endTimestamp', 'sonata_type_datetime_picker', array(
'widget' => 'single_text',
'label' => 'sip_event_end_timestamp',
'dp_side_by_side' => true,
'dp_use_seconds' => false,
'model_timezone' => 'Europe/Moscow',
'view_timezone' => 'Europe/Minsk',
'format' => 'dd.MM.yyyy HH:mm'
)
)

A2LiX Translation Form labels options

I'm using KnpLabs/DoctrineBehaviors/Translatable and A2LiX Translation Form to translate my entities in a Symfony application. It works very well. However, when the form is rendered there is a "translations" title that I would like to delete and a "EN [Default]" text on a tab that I would like to change.
In the examples of the doc, there's a "medias" example so I imagine that we can change this text. Moreover, the tabs don't have this [Default] text. So I imagine that's possible to change them.
And this is mine:
Does anybody know how to do it? If we take a look on the form type options we don't see anything concerning the "Translations" label. For the "Default", I can't see where I should search for it.
Default template file is located at vendor/a2lix/translation-form-bundle/A2lix/TranslationFormBundle/Resources/views/default.html.twig. If you want you can specify your own template and set it in config.yml file, like this:
a2lix_translation_form:
....
templating: "#SLCore/includes/translation.html.twig"
More information can be found here.
For the "translations" title, I was able to override it adding a label to the form type just like a normal field. However, it is not possible to use a blank value. I had to use ' ' in order to override the text.
->add('translations', 'a2lix_translations', array(
'label' => ' ', --> this overrides the translations title
'fields' => array(
'name' => array(
'field_type' => 'text',
'label' => 'blabla'
),
'description' => array(
'field_type' => 'textarea',
'label' => 'bleble',
)
)
))
For the "Default" label, I still have no solution.

Symfony Form: translation of attr

I'm trying to translate help messages that I created on my form using attr array but I can't translate it. Does anybody know how to do it?
This is an example:
->add('facebook_sharing_title', null,
array(
'label' => "establishment.edit.facebooksharingtitle",
'required' => false,
'attr' => array('help' => '70 caractères max. conseillé')
)
)
As you can see, the translation o the label is ok. However, if I do the same for help it doesn't work. I can't find anything about it in the symfony site. I know how to do it on twig but I would like to do it on the form type.

Custom Symfony ChoiceList way of rendering choices (grouped in divs)

I have a choice form field that has a lot of options that I need to group somehow so that they will be rendered divided in groups, maybe placed inside different divs.
I'm currently trying to implement the ChoiceListInterface to achieve that, but I don't know how to implement the methods so I can render the choices divided by groups, but the docs does not clarify how to do that..
The choices always get rendered together.
You have this array
$grouped_choices = array(
'Swedish Cars' => array(
'volvo' => 'Volvo',
'saab' => 'Saab',
),
'German Cars' => array(
'mercedes' => 'Mercedes',
'audi' => 'Audi'
)
);
First way: quick and simple
$builder->add($name, 'choice', array(
'choices' => $grouped_choices),
)
But I don't thinks it works with 'expanded' => true
So there is another way, more customizable (maybe more dirty)
In your FormType
foreach($grouped_choices as $name => $choices) {
$builder->add($name, 'choice', array(
'choices' => $choices),
'expanded' => true, //custom the widgets like you wants
);
}
Let the controller send the array to the view, then in the view
{% for name, choices in grouped_choices %}
<div class="whatever">
{{ form_row(name) }}
</div>
{% endfor %}

How to disable HTML escaping of labels in KnpMenuBundle

I want to render an HTML label like:
$menu->addChild('Dashboard', array(
'route' => 'dashboard',
'label' => '<i class="fa-icon-bar-chart"></i><span class="hidden-tablet"> Dashboard</span></a>',
'extra' => array('safe_label' => true)
)
);
And I've pass the proper option while rendering:
{{ knp_menu_render('WshCmsHtmlBundle:Builder:mainMenu', {'allow_safe_labels': true} ) }}
But my label is still being escaped. What am I doing wrong?
Ok, the answer is!
You set up extra items on menu item not by 'extra' key but by 'extras' key.
So when you setup the item like this:
$menu->addChild('Dashboard', array(
'route' => 'dashboard',
'label' => '<i class="fa-icon-bar-chart"></i><span class="hidden-tablet"> Dashboard</span></a>',
'extras' => array('safe_label' => true)
)
);
it works fine!
There's two steps to achieve this.
1. MenuBuilder
You have to set safe_label to true in extras. Note that you can now write HTML in your label.
$menu->addChild('Home<i><b></b></i>', array(
'route' => 'homepage',
'extras' => array(
'safe_label' => true
),
));
2. Twig
You have to filter the output of knp_menu_render() so that it prints raw HTML (see documentation).
{{ knp_menu_render('main', {'allow_safe_labels': true}) | raw }}
Warning
Please be aware that this may be dangerous. From the documentation:
Use it with caution as it can create some XSS holes in your application if the label is coming from the user.
I used FyodorX's method to add a strong tag. It works like a charm but I must say that the raw filter is not necessary

Resources