setting a placeholder attribute with translation in Symfony2 form input

42,046

Solution 1

In Twig you shouldn't put {{ within {{ (same for {%); think of it as the php tag.

The following should work

{% set usernameplaceholder = 'security.login.usernameplaceholder'|trans %}
{{ form_widget(form.username, { 'attr': {'class': "span12",
    'placeholder': usernameplaceholder} }) }}

OR

{{ form_widget(form.username, { 'attr': {'class': "span12",
    'placeholder': 'security.login.usernameplaceholder'|trans} }) }}

Solution 2

For Symfony 3.x, 4.x

Another way to add placeholders (or any attributes for that matter) is by passing an options-array to the form $builder containing another Array attr with attributes as key-value pairs.

// The parameters are column name, form-type and options-array respectively.
$builder->add('field', null, array(
            'attr' => array(
                 'placeholder' => 'support.contact.titleplaceholder'
             )
        ));

Solution 3

You can translate this way as well (Using symfony4) in twig: In a form placeholder wich would be written like this:

{'attr':{'placeholder': "Text to translate"}}

As for a placeholder in html wich would be written like this, you can translate this way:

<input placeholder="{{"Text to translate"|trans }}">
Share:
42,046
Amine Jallouli
Author by

Amine Jallouli

BY DAY: I am statistics engineer. BY NIGHT: I am an IT engineer &amp; Freelance web developer. Started my adventure with Symfony / Bootstrap / jQuery since end 2012 and recently angularJS I can not live without git. I love Google Cloud Platform and used to it. I hate MS Windows &amp; I love Linux Distro. MS Windows disappeared from my laptop since 2008 &amp; actually using Ubuntu 16.04 and Debian 8.2. Future entrepreneur... ;)

Updated on November 23, 2021

Comments

  • Amine Jallouli
    Amine Jallouli over 2 years

    I am using FOSUserBundle for managing my users. In order to register user, I reused the form of the bundle which meets my needs. Nevertheless, I needed to set some attributes of my fields. This is was done easyly by twig like this:

        {{ form_widget(form.username, { 'attr': {'class': "span12",
            'placeholder': "Username"} }) }}
    

    Now, my goal is to make automatic translation on my placeholder, so I proposed this code:

        {{ form_widget(form.username, { 'attr': {'class': "span12",
            'placeholder': "{{'security.login.usernameplaceholder'|trans}}"} }) }}
    

    This previous code produced an input with placeholder value equal to {{'security.login.usernameplaceholder'|trans}}

    To get rid of this problem, I tried to set variable for that but symfony generated an error!!!

        {% set usernameplaceholder = {{'security.login.usernameplaceholder'|trans}} %}
        {{ form_widget(form.username, { 'attr': {'class': "span12",
            'placeholder': usernameplaceholder} }) }}
    

    Is there any proposition to solve this problem?

    Thanks,

  • numediaweb
    numediaweb almost 8 years
    placeholder should be inside the "attr" like: 'label' => false, 'attr' => array('placeholder' => 'support.contact.titleplaceholder'..
  • MaximeW
    MaximeW over 6 years
    I did a little testing in Symfony 3.4 and it seems that this will not be translated automatically. Any suggestions on how to get the translation with this approach?
  • mr1031011
    mr1031011 about 6 years
    I wonder about this as well, does seem to get auto translated
  • Niket Pathak
    Niket Pathak about 6 years
    you could add in another attribute 'translation_domain' => 'fooo' where fooo refers to your translation file fooo.de.xlf as per this SO post
  • crmpicco
    crmpicco over 2 years
    @numediaweb Updated