Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] 4.2+ form type extensions configuration does not consider using a parameter as the given class #31052

Closed
@antoniocambados

Description

@antoniocambados

Symfony version(s) affected: 4.2+

Description
When using a parameter as the class given to a service Definition, the FormPass will trigger a deprecation warning or throw an Exception for not being able to ensure method getExtendedTypes exists.

How to reproduce

Create a form type extension:

// src/App/Form/Extension/FooFormExtension
namespace App\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FormType;

class FooFormExtension extends AbstractTypeExtension
{
    public static function getExtendedTypes(): iterable
    {
        return [FormType::class];
    }
}

Set up the configuration:

# config/services.yaml
parameters:
    foo_form_service_class: 'App\Form\Extension\FooFormExtension'
services:
    foo_form_service:
        class: '%foo_form_service_class%'
        tags:
            - { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\FormType }

Now, when running your app, you'lle get this deprecation warning:

Not implementing the static getExtendedTypes() method in %foo_form_service_class% when implementing the Symfony\Component\Form\FormTypeExtensionInterface is deprecated since Symfony 4.2. The method will be added to the interface in 5.0.

If you set up the service without a backwards-compatible definition such as:

# config/services.yaml
parameters:
    foo_form_service_class: 'App\Form\Extension\FooFormExtension'
services:
    foo_form_service:
        class: '%foo_form_service_class%'
        tags: ['form.type_extension']

Then an exception will be thrown:

(1/1) InvalidArgumentException "form.type_extension" tagged services have to implement the static getExtendedTypes() method. The class for service "foo_form_service" does not implement it.

Possible Solution
Other than trying to resolve the class as a parameter in Symfony\Component\Form\DependencyInjection\FormPass, it could be possible to deprecate using parameters to provide the class of a service altogether.

Additional context
This is all happening in Symfony\Component\Form\DependencyInjection\FormPass::processFormTypeExtensions().

I've noticed this while using LexikFormFilterBundle.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions