As per 本文 http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html在 Symfony 文档上,我创建了一个自定义字段类型,将其设置为services.yml
,并且我能够成功地使用它。
例如,我创建一个名为的自定义字段customdate
如下,效果完美:
# 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 }
但是,如果我尝试将自定义字段命名为date
(这与现有的 Symfony 字段类型 http://symfony.com/doc/current/reference/forms/types/date.html,因为这是我试图覆盖的),如下所示,然后 Symfony 完全忽略我的自定义字段,并默认为内置的 Symfonydate
字段类型改为:
# 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 }
我已经检查过我的getName()
函数返回正确的名称,与我在中提供的别名相匹配services.yml
.
我使用上述服务的代码如下。
这有效:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('date', 'customdate')));
}
这不起作用:(或者更确切地说,Symfony 使用内置字段类型而不是我的)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('date', 'date')));
}
我应该注意,如果我将“customdate”或“date”替换为手动创建的对象,例如new Date()
然后就可以正常工作了。问题似乎具体在于 Symfony 更喜欢其内置字段类型而不是中指定的字段类型services.yml
.
我的问题:有没有办法用同名的自定义字段类型覆盖内置的 Symfony 字段类型?显然,根据我上面的描述,Symfony 似乎会忽略任何与内置 Symfony 字段类型同名的自定义字段。有没有办法解决?