As per this article on the Symfony docs, I've created a custom field type, set it up in services.yml
, and I am able to use it successfully.
For example, I create a custom field named customdate
as follows, which works perfectly:
# src/Acme/DemoBundle/Resources/config/services.yml
services:
acme_demo.form.type.date:
class: Acme\DemoBundle\Form\Type\DateType
tags:
- { name: form.type, alias: customdate }
However, if I try to name my custom field as date
(which is the same as an existing Symfony field type, since this is what I am trying to override), as shown below, then Symfony completely ignores my custom field, and defaults to the built-in Symfony date
field type instead:
# src/Acme/DemoBundle/Resources/config/services.yml
services:
acme_demo.form.type.date:
class: Acme\DemoBundle\Form\Type\DateType
tags:
- { name: form.type, alias: date }
I've checked that my getName()
function returns the correct name, matching up with the alias I provided in services.yml
.
The code in which I make use of the above services follows below.
This works:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('date', 'customdate')));
}
This does not work: (or rather, Symfony uses the built-in field type instead of mine)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('date', 'date')));
}
I should note that if I replace the 'customdate' or 'date' with a manually-created object such as new Date()
then it works fine. The problem appears to be specifically that Symfony prefers its built-in field types over the ones that are specified in services.yml
.
My question: is there any way to override built-in Symfony field types with custom field types that have the same name? Clearly, from what I described above, Symfony appears to ignore any custom fields that are aliased to the same name as a built-in Symfony field type. Is there any way around this?