Sylius/SyliusResourceBundle

Call to undefined method createBuilder

pdias opened this issue · 3 comments

pdias commented

Sylius version affected: 1.11.x

Symfony: 6.3

Description
When i try to use this code:

`class Security2faController extends AbstractController
{
use TargetPathTrait;

#[IsGranted('ROLE_USER')]
public function enable2fa(Request $request, TotpAuthenticatorInterface $totpAuthenticator, EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
{
    /** @var User $user */
    $user = $this->getUser();
    if (!$user->isTotpAuthenticationEnabled()) {
        $user->setTotpSecret($totpAuthenticator->generateSecret());
        $entityManager->flush();
    }

    $form = $this->createFormBuilder([])
        ->add('code', TextType::class, [
            'constraints' => [
                new NotBlank(),
                new Length(['min' => 6]),
            ],
            'attr' => [
                'placeholder' => $translator->trans('security.2fa.code.placeholder', [], 'my.user'),
            ],
        ])
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // data is an array with "code" key
        $data = $form->getData();
        if ($totpAuthenticator->checkCode($user, $data['code'])) {
            $user->setTotpEnabled(true);
            $entityManager->flush();

            // User force logout
            $request->getSession()->invalidate();
            $tokenStorage->setToken(null);

            return $this->redirectToRoute('security_login');
        }

        $form->get('code')->addError(new FormError($translator->trans('security.2fa.invalid.code', [], 'my.user')));
    }

    return $this->render('@Security/Security/enable2fa.html.twig', [
        'form' => $form->createView()
    ]);
}

}`

In this line:

`...

$form = $this->createFormBuilder([])

...`

This error appears:

Call to undefined method Sylius\Component\Resource\Symfony\Form\Factory\FormFactory::createBuilder()

Resource bundle, if you look at the services declaration inside Resources/config/services/form.xml creates a global alias for the Symfony FormFactoryInterface with this line

<service id="Symfony\Component\Form\FormFactoryInterface" alias="sylius.form.factory" />

so standard symfony controllers will use the Sylius form factory, instead of the Symfony one, if you try to use DI and autowiring with the interface. Long story short, nothing will work anymore.
To fix that, you can create an overriding alias in config/services.yaml like this one:

services:
    Symfony\Component\Form\FormFactoryInterface: '@form.factory'

this should fix things on the "symfony-side", and i use this fix in a couple of project where i use this bundle. To be honest though, i have no idea why there's this global alias at the Sylius level and if overriding it will break something on the Sylius side (i haven't had any problem as of today). This is something that the developers of this bundle should know.

Hum my bad, this has been fixed few days ago but not released yet.

If it has already been fixed I will close it and wait for it to be released.

Thanks.