Let's say I create custom form types as services, as it is described in the Symfony documentation. But I want 2 "gender" custom types, with 2 different input parameters, which I was doing like this in Symfony 2.7:
# app/config/config.yml
parameters:
genders1:
m: Male
f: Female
genders2: # This makes no sense at all, but it is for the example purpose!
h: Horse
t: Turtle
And then, I was declaring 2 services like this:
<!-- src/AppBundle/Resources/config/services.xml -->
<service id="app.form.type.gender1" class="AppBundle\Form\Type\GenderType">
<argument>%genders1%</argument>
<tag name="form.type" alias="gender1" />
</service>
<service id="app.form.type.gender2" class="AppBundle\Form\Type\GenderType">
<argument>%genders2%</argument>
<tag name="form.type" alias="gender2" />
</service>
As you can see, I was using the same GenderType
class for 2 custom form types (with the gender1
and gender2
aliases), which I could use like this:
$builder
->add('field1', 'gender1')
->add('field2', 'gender2');
This allowed me to add some common logic in only one class (GenderType
) with different input parameters (I have many more possibilities than 2 in this example).
But as of Symfony 2.8, adding a field using the service alias is deprecated. The class name has to be passed as the second argument instead, like this:
$builder->add('field1', GenderType::class)
So how can I make the difference between my 2 services (each of them not having the same input parameters)?
Creating Gender1Type
and Gender2Type
extending an abstract GenderType
class would be really painful, as I would have to create a lot of classes with empty content.
Do you have any idea on how to implement my pattern in Symfony 2.8, keeping services with different input parameters, but not creating a lot of classes?