Jump to content

Translations in Symfony forms


arivei

Recommended Posts

What is the proper way to use translator with Symfony forms?

I managed to inject translator service into my FormType, but then I cannot call function  $this->createForm(FormType::class).

 

Minimal example:

# config.services.yml
services:
  _defaults:
    public: true

  someservice:
    class: class\FormType
    public: true
    arguments:
      - "@translator"

# module/src/Forms/FormType.php
class FormType extends AbstractType
{
  /** @var TranslatorInterface */
  private $translator;

  public function __construct(TranslatorInterface $translator)
  {
      $this->translator = $translator;
  }
}

# module/src/Controller/MyController.php
class MyController extends FrameworkBundleAdminController
{
	public function config(Request $request)
    {
		// i get service here, works fine
        $service = $this->get('someservice');
		
		// cannot instantiate form
		// $config = $this->createForm(FormType::class);

	return $this->render('@Modules/something/index.html.twig', 
    [
        'config' => $config->createView()
    ]);
	}

}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...