-1

I am migrating an application from Symfony 4.4 to Symfony 5.4. In the case of forms, I have a form that I pass in the constructor a variable ContainerInterface $container to use a parameter in the configuration of a widget. In version 5.4 this gives me a deprecation warning:

Since symfony/dependency-injection 5.1: The "Symfony\Component\DependencyInjection\ContainerInterface" autowiring alias is deprecated. Define it explicitly in your app if you want to keep using it. It is being referenced by the "App\Form\BuscarAvanzadaNinhoType" service.

How do I solve it?

class BuscarAvanzadaNinhoType extends AbstractType {

    private $em;
    private $security;
    private $uuidEncoder;
    private $container;

    public function __construct(ManagerRegistry $em, Security $security, UuidEncoder $uuidEncoder, ContainerInterface $container) {
        $this->em = $em;
        $this->security = $security;
        $this->uuidEncoder = $uuidEncoder;
        $this->container = $container;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
               
                ->add('otrosAspectos', Select2EntityType::class, array(
                    'class' => EtiquetaAspectoNinho::class,
                    'remote_route' => 'ninhos_encontrarEtiquetaAspectos',
                    'primary_key' => 'id',
                    'text_property' => 'text',
                    'multiple' => true,
                    'allow_clear' => false,
                    'delay' => 250,
                    'cache' => false,
                    'minimum_input_length' => 3,
                    'scroll' => true,
                    'page_limit' => $this->container->getParameter('limite_resultados_etiquetas'),
                    'language' => 'es',
                    'width' => '100%',
                    'placeholder' => '',
                        )
        );

        
    }

    

}
Community
  • 1
  • 1
Francisco
  • 194
  • 2
  • 9
  • 1
    As usual: you should not inject the whole container, but whatever you need from the container. Injecting the **whole** container is discouraged since years – Nico Haase Feb 01 '22 at 08:37

2 Answers2

1

You can pass parameters directly without the container.

public function __construct(ManagerRegistry $em, Security $security, UuidEncoder $uuidEncoder, array $formParams) {
    $this->em = $em;
    $this->security = $security;
    $this->uuidEncoder = $uuidEncoder;
    $this->formParams = $formParams;
}

services.yml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        bind:
            $formParams:
                limite_resultados_etiquetas: '%limite_resultados_etiquetas%'

In the form:

'page_limit' => $this->formParams['limite_resultados_etiquetas'],
Artem
  • 1,426
  • 12
  • 17
-1

Instead of using ContainerInterface, use ContainerBagInterface

Micka Bup
  • 391
  • 1
  • 9