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
Related
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
))
......
......
;
How can I change the validator language messages. For example if I want to change the 'required' text 'The kilometerszakelijk field is required.' to something else?
All language specific content is stored in your resources/lang// folder. One of the files you'll find in this folder is validation.php that holds validation messages for given language, so if you want to change the translations, you'll need to edit that file.
Depending on the language that is used by the application, messages will be read from different folders in resources/lang/. You can set language/locale using
App::setLocale('en');
You can also set fallback locale (that is used when there are no translations for current language) by setting it in your config/app.php file, e.g.:
'fallback_locale' => 'en',
See more details in the docs: http://laravel.com/docs/5.0/localization
Create a folder named like your language code in
resources/lang
then create a file in this folder called validation.php and in there, write something like
return [
'required' => 'Das Feld :attribute ist ein Pflichtfeld.',
]
For the default messages, you can adjust the translations in resources/lang/<language>/validation.php.
You can also pass entirely custom messages to the validator.
Laravel-lang
In this repository, you can find the lang files for the framework PHP, Laravel 4&5.
you can see here for use that.
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
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**} }) }}
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.