Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service "form.factory" not found when calling createForm

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?

like image 777
Marcel Vigneault Avatar asked Mar 04 '19 18:03

Marcel Vigneault


1 Answers

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
like image 97
vigneault.charles Avatar answered Oct 11 '22 07:10

vigneault.charles