Modify choices labels in a Twig template - php

I'm trying to modify in my Twig template the labels of the choices that I have for a choice field in my form. I create my field as usual in PHP:
->add('registered','choice',array('choices' => array('1' => 'Yes', '0' => 'No'),'expanded' => true))
and then in my template if a condition is true then I want to do something like this:
{{ form_widget(form.registered,{'choices':{'1':'Ναι','0':'Όχι'}})}}
So the idea would be that instead of Yes and No, I would get it in Greek. My problem is that I can't figure out the right attribute to pass to the form_widget function, I've tried with 'choices' but it doesn't seem to work.

Put Yes and No in a global message translation domain and Symfony will translate them automatically. In 2.1 you will be able to put translations for entities into different domains, not only into default one.

Related

Do forms automatically use translation in Symfony 4?

I'm working on upgrading an app from Symfony 3 to Symfony 4.
I noticed some errors in the profiler in the new version on one of my routes that has a form. The errors were in Translation -> Translation Messages -> Missing:
These messages are not available for the given locale and cannot be found in the fallback locales. Add them to the translation catalogue to avoid Symfony outputting untranslated contents.
Every form field was listed under missing messages.
I hadn't done anything with translation in the old version, so I was wondering why the new version was expecting translations. Is that something that's automatically associated with forms now? If so, is there a way to turn that off? It really isn't necessary for this app.
When you enable the translations in the framework bundle Forms will try to use them via the TranslationExtension that is automatically registered. If you don't need any translations you can disable them. Be aware that validation errors on the form are returned as their translation keys and not the message then.
In Symfony 3 the setting should be in app/config/config.yml and in Symfony 4 directory structure they should be in either config/packages/framework.yaml or config/packages/translation.yaml under:
framework:
translator: ~ # just set this to false if you don't want any translations to be used
Your other option is to prevent the TranslationExtension from being registered or write a custom form extension that sets the translation_domain on the abstract FormType to false.
Another option is to just ignore the notices for missing translations. Since translations are cached and will always fall back to the key (in your case the actual label) it will not have any performance impact or other negative effects.
edit: Regarding your final question, I don't think this behavior changed much from Symfony 3 to 4 and you should have seen a similar behavior before. I'm guessing you just didn't notice the warning before, but it was there. That's just a guess though.
You should disable Translation in Form like follow:
$builder
......
......
->add('budget', MoneyType::class, array(
'label_attr' => array('class' => 'control-label'),
'attr' => array('class' => 'span11'),
...
'translation_domain' => false
))
......
......
;

yii activeform label translation from different translation-file

I need to translate a label of an active form without changing the model.
This:
$form->label($model, 'myField', array('class' => 'title'))
Takes the translation by default from the translation.php in the protected/messages/lang/ folder.
Now I want the label to take the translation from another file (without changing the model).
How would I do that? The docs are a little vague on what kind of variables I can pass to the label...
You can specify label in the $htmlOptions array:
http://www.yiiframework.com/doc/api/1.1/CHtml#activeLabel-detail
$form->label(
$model,
'myField',
array('class' => 'title', 'label' => Yii::t('myCategory', 'Field label'))
)
Yii::t() method translates the given message from source language to target language. You can read more information about internationalization here:
http://www.yiiframework.com/doc/api/1.1/YiiBase#t-detail
http://www.yiiframework.com/doc/guide/1.1/en/topics.i18n

Dynamically setting class attribute to a symfony2 choice list generated from an entity in FormBuilder

I'm wondering how can I dynamically set the HTML class attribute to checkbox elements using FormBuilder in Symfony2.
I'm using an element generated by an entity :
add('myEntity', 'entity', array(
'class' => 'MyBundle:myEntity',
'property' => 'label',
'multiple' => true,
'expanded' => true,
))
This only sets the id of the checkboxes according to this template : [form_name]_[class_name]_[id] (for example acme_my_form_my_entity_5)
I want to know if I can also set the class attribute with these values : [form_name][class_name][label]. Label is another field in the entity.
Is this possible ?
Yes. Is possible, but you will need to read the Docs!
Form Customization
If you are lazy and do not want to read them, then just search google using "custom checkboxes symfony" and you will come back to Stackoverflow, where there are many other examples like this. Replying with code will be just a copy of another examples, so you better build your own customized one and paste it as your own solution.
If you want an easy solution, keep in mind you can to something like this directly in your Twig template:
{{ form_widget(form.myEntity, { 'attr': {'class': **Your_dinamic_class_value**} }) }}

Laravel 4 Populate Form fields with a Model but edit the field first

I am working with PHP and Laravel 4. Using the Form method to populate an edit form with a Model like below...
Form::model($timecard, array('route' => array("admin/timecard/edit", $id)))
My problem is, some of the text fields get populated with DateTime values from the Database and I need to be able to run some code on these certain fields before it populates the Form field.
Any ideas how to do that or if it's possible to do that while still using the Model to auto-fill the Form fields?
For example this form field below gets filed with a GET value, otherwise it gets field with the Data from the Database for column clock_in_datetime however I would like to run a PHP function on this field before it fills the form so that I can apply TimeZone or other formatting to it...
{{ Form::text("clock_in_datetime", Input::get("clock_in_datetime"), array(
"placeholder" => "2013-09-04 14:22:35",
'class' => 'form-control'
)) }}
I believe you can do the following:
{{ Form::text("clock_in_datetime", yourFormattingFunction(Form::getValueAttribute("clock_in_datetime")), array(
"placeholder" => "2013-09-04 14:22:35",
'class' => 'form-control'
)) }}
Form::getValueAttribute() is Laravel's way of deciding which value to use (previous Input, Session or Model). So you can apply your formatting function to the output of this function.
http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#751-773

In Symfony2, how do I get labels from the FormBuilder

In Symfony2, I'm using formbuilder. I'm setting the labels in the form, as per the documentation.
However, when I'm on the 'show' and 'index' pages, I have to copy the labels into Twig.
Is there a way to have the same labels used everywhere? The options I have thought of:
Access the formbuilder configuration, but without actually building the form
Have a central config file, and lookup from the formbuilder and the twig files into that file
However, either way requires me to 'do' something, which I'm not used to in Symfony. It seems like this is something that would already have been solved, but I'm not sure how.
You can utilize translation system to overcome this problem. Make sure that you have enabled translation in config.yml.
If you have added field in your formtype like this
$builder->add('title', 'text', array(
'label'=> 'model.title'
));
//.....
Create a file named messages.en.yml in your bundles Resources/translations directory (replace en with your default locale and create multiple files based on the locales. Check translation chapter of the book.) and put following
#src/YourBundle/Resources/translation/messages.en.yml
model:
title: "Title"
field: "Field"
#....
Add and edit forms label will show Title. In index and show pages you can do
{{ "model.title" | trans([], 'messages') }}
Though this process is a bit lengthy but it is one time and you can change the value of the labels by changing the translation files.
I just want to say something about #Mun Mun Das's answer.
In Symfony 6, it looks like we don't even need to use trans() anymore in the template in the form. I defined the label in the Type file (php) and it translates directly. I don't know why though.
Example
FormatType
#src/Form/UserType.php
$builder->add('uEmail', TextType::class, array(
'label'=> 'uEmail' ,
'attr' => [ 'maxlength' => 50]
))
Translation file
#translations/message.en.yaml
uEmail: Email
It automatically translates it. I guess Symfony try to translate it by default ? If label is not set in UserType.php or in the template with form_label() function, it will display "u Email" by default

Categories