6

I follow a class where there's an example how to make a form. When I try it I get a ServiceNotFoundException error and I can't find it anywhere on the web.

Service "form.factory" not found: the container inside "App\Controller\MyTestController" is a smaller service locator that only knows about the "doctrine", "http_kernel", "parameter_bag", "request_stack", "router", "session" and "twig" services.

When I look at the documentation I followed every step and make a dummy example and I still get the error. https://symfony.com/doc/current/best_practices/forms.html

I get this error when I call in my Controller.php the function createForm():

public function add($id, Request $request)
{
    $ads = new Ads();
    $form = $this->createForm(AdsType::class, $ads);
}

I Ctrl-F the "form.factory" alias in my whole project and it look setup.

The function into the ControllerTrait:

    C:\Users\Marcel\Documents\Projects\PHP\Symfony\OpenClassRoom\vendor\symfony\framework-bundle\Controller\ControllerTrait.php:
  314      protected function createForm(string $type, $data = null, array $options = []): FormInterface
  315      {
  316:         return $this->container->get('form.factory')->create($type, $data, $options);
  317      }
  318  
  ...
  324      protected function createFormBuilder($data = null, array $options = []): FormBuilderInterface
  325      {
  326:         return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options);
  327      }

And in the AbstractController:

C:\Users\Marcel\Documents\Projects\PHP\Symfony\OpenClassRoom\vendor\symfony\framework-bundle\Controller\AbstractController.php:
       84              'twig' => '?'.Environment::class,
       85              'doctrine' => '?'.ManagerRegistry::class,
       86:             'form.factory' => '?'.FormFactoryInterface::class,
       87              'security.token_storage' => '?'.TokenStorageInterface::class,
       88              'security.csrf.token_manager' => '?'.CsrfTokenManagerInterface::class,

Is it still valid in Symfony 4.2?

  • Should still work in 4.2. Maybe try: "composer require symfony/form" from the command line. – Cerad Mar 04 '19 at 19:00

1 Answers1

10

It's not deprecated but it is not installed by default since Symfony 4.0 or 4.1 so you need to do install it with the composer now.

composer require symfony/form