Search code examples
symfony-formssymfony4formbuilder

How to Fix "Attempted to call an undefined method named "createNamedBuilder..."?


I'm trying to create a named form (builder) within my controller like

...
$form = $this->createNamedBuilder('form1', $data)
->add(...)
->getForm();

But i get the title-mentioned error. When i check the abstract controller trait class there are no createNamed() or createNamedBuilder() functions in there.

How do i create a named form with a form builder?

Kind Regards


Solution

  • According to This you need to acquire the FormFactory(Interface via Dependency Injection).

    Adding FormFactoryInterface $formFactory to my controller method's parameters and using it like

    $formFactory->createNamedBuilder('name', FormType::class, $data)...
    

    did the trick.