Symfony2:使用具有相同名称的自定义字段类型覆盖内置字段类型 [英] Symfony2: Overriding a built-in field type with a custom field type having the same name

查看:27
本文介绍了Symfony2:使用具有相同名称的自定义字段类型覆盖内置字段类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据关于 Symfony 文档的这篇文章,我已经创建了自定义字段类型,在services.yml中设置,我就可以成功使用了.

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.

例如,我创建了一个名为 customdate 的自定义字段,如下所示,效果很好:

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 }

但是,如果我尝试将自定义字段命名为 date(与 一个现有的 Symfony 字段类型,因为这是我试图覆盖的),如下所示,然后 Symfony 完全忽略我的自定义字段,并默认为内置 -在 Symfony date 字段中输入:

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 }

我已经检查过我的 getName() 函数返回了正确的名称,与我在 services.yml 中提供的别名相匹配.

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.

这有效:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('date', 'customdate')));
}

这不起作用:(或者更确切地说,Symfony 使用内置字段类型而不是我的)

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')));
}

我应该注意,如果我用手动创建的对象(例如 new Date() 替换customdate"或date",则它可以正常工作.问题似乎特别在于 Symfony 更喜欢其内置字段类型,而不是 services.yml 中指定的字段类型.

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.

我的问题:有什么方法可以用具有相同名称的自定义字段类型覆盖内置的 Symfony 字段类型?显然,根据我上面的描述,Symfony 似乎忽略了任何别名与内置 Symfony 字段类型同名的自定义字段.有没有办法解决这个问题?

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?

推荐答案

据我所知,没有办法真正覆盖基本字段类型,您可以继承它们并使用您自己的名称.

As far as i know there is no way to truly override the base field types, you can inherit them and use your own name.

但是,如果您要覆盖的字段类型没有提供您认为应该报告的该类型可能存在的问题.

However if the field type that you want to override is not providing the functionality you think it should there is likely an issue with that type that should be reported.

对于您的情况,日期类型不采用典型的 php date() 格式字符串.从查看文档这里 我们看到日期格式是由 IntlDateFormatter 类解析的.有关有效格式,请查看此列表.

For your case the date type doesnt take typical php date() format string. From looking at the documentation here we see that the date format is parsed by the IntlDateFormatter class. For valid formats check out this list.

要完成您想要的格式 date('d M Y'),您可以使用:

To accomplish the format you want date('d M Y') you would use:

$builder->add('my_date_field', 'date', array(
    'format'=>'d MMM Y'
));

这篇关于Symfony2:使用具有相同名称的自定义字段类型覆盖内置字段类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆