felipep
felipep

Reputation: 2512

Symfony2 Form use translator for attr attribute

I have a form with some elements for one element I want to use the translator inside an attribute(data-placeholder that it's the name for a JQuery Extension).

$builder->add('items', 'entity', array(
    'label' => 'MyBundle.items',
    'required' => true,
    'em' => $this->em,
    'multiple' => true,
    'expanded' => false,
    'attr' => array(
         'placeholder' => 'MyBundle.items.placeholder',
         'data-placeholder' => 'MyBundle.items.placeholder.data',
    ),
));

Is it possible to use the translator for such an attribute?, for placeholder or for label it's so possible

There is a twig solution for this problem, but I would like to know a solution inside the Entity Type.

If someone needs the Twig Solution:

 {{ form_widget(form.items, {'attr': {'data-placeholder': 'MyBundle.items.placeholder.data'|trans } })  }}

Upvotes: 3

Views: 4529

Answers (2)

Oldskool
Oldskool

Reputation: 34837

I came upon the same situation today and used a slightly different approach than Shushant's way. I directly injected the Translator into the FormType (as previously suggested by zelazowy in the comments as well). So I ended up with this:

<?php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;

class TranslatableType extends AbstractType
{
    /**
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @param TranslatorInterface $translator
     */
    public function __construct(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }
}

With the corresponding service:

app.form.translatable:
    class: AppBundle\Form\Type\TranslatableType
    arguments:
      - @translator

To create the translatable labels, you can then directly call $this->translator->trans() in the FormType, like so:

$builder->add('field', 'choice', array(
    'choices' => array(
        1 => $this->translator->trans(
            'forms.choices.field.1', null, 'forms'
        ),
        2 => $this->translator->trans(
            'forms.choices.field.2', null, 'forms'
        ),
    )
));

Make sure to call the service in your Controller when creating the form, like this:

$form = $this->createForm($this->get('app.form.translatable'));

That way, everything should be translated just fine.

Upvotes: 4

Shushant
Shushant

Reputation: 1635

Well if you are not talking about translating via annotation then here is what you actually want to do isn't it ?

class SomeFormType extends AbstractType {

   protected $container;

  public function __construct($container){

      $this->container = $container;
 }

  public function buildForm(FormBuilderInterface $builder, array $options){

    $trans = $this->container->get('translator');
    $builder->add('items', 'entity', array(
      'label' => 'MyBundle.items',
       required' => true,
       'em' => $this->em,
       'multiple' => 'true',
       'expanded' => false,
       'attr' => array(
          'placeholder' => $trans->translate('MyBundle.items.placeholder'),
          'data-placeholder' => $trans->translate('MyBundle.items.placeholder.data'),
    ),
   ));
  }
}

Service

  //resources/config/services.yml
  services:
      form.serivce:
          class: SomeBundle\Form\SomeFormType;
          arguments:    ["@service_container"]

Upvotes: 4

Related Questions