Injecting Translator service to FormType
symfony
Solution
Although I'd like to know how to solve the injecting issue, I finally managed to find a better solution, just simply overriding the form error rendering by Twig including this block on my custom twig theme, including the trans filter.
{% block form_errors %}
{% spaceless %}
{% if errors|length > 0 %}
{#
<ul>
{% for error in errors %}
<li>{{ error.message }}</li>
{% endfor %}
</ul>#}
{% for error in errors %}
<div class="field_error">{{ error.message |trans}}</div>
{% endfor %}
{% endif %}
{% endspaceless %}
{% endblock form_errors %}
Problem
I'm trying to find a the most reusable working option for being able to translate from a FormType. My first option is to declare a service specifically for each FormType this way: services.yml ``` form.enquiry: class: Acme\DemoBundle\Form\EnquiryType arguments: [@translator] ``` EnquiryType.php ``` use Symfony\Component\Translation\Translator; class EnquiryType extends AbstractType { public $translator; public function __construct(Translator $translator=null) { $this->translator = $translator; } public function buildForm(FormBuilderInterface $builder, array $options) { $tr= $this->translator; $msg=$tr->trans('default_error'); $builder->add ... ``` MyController.php ``` $form = $this->container->get('form.enquiry')->create(); return $this->render('AcmeDemoBundle:Home:index.html.twig', array( 'form' => $form->createView() )); ``` gives this error FatalErrorException: Error: Call to undefined method Acme\DemoBundle\Form\EnquiryType::create() I'd like to know how to solve it by changing the code or even better finding a better option that allows me to inject the translator service to any FormType without needing to declare each FormType service individually.