【问题标题】:Symfony2 - The type name specified for the service does not matchSymfony2 - 为服务指定的类型名称不匹配
【发布时间】:2015-12-12 15:38:34
【问题描述】:

我尝试使用我自己的 ImageType 配置 EasyAdminBundle 以在我的 ArticleType 中上传文件。 所以我在app\config\services.yml 创建了这个服务:

services:
    oah_news_form_type_image: 
        class: OAH\NewsBundle\Form\ImageType
        tags: [ { name: form.type, alias: 'oah_news_form_type_image' } ]

并尝试在我的app\config\config.yml 中调用它:

easy_admin:
entities:
        Article:
            class: OAH\NewsBundle\Entity\Article
            form:
                fields:
                    - Titre
                    - Auteur
                    - Date
                    - Categorie
                    - { property : 'Image', type : oah_news_form_type_image}

但我有以下错误,我不知道如何解决它:

The type name specified for the service "oah_news_form_type_image" does not match the actual name. Expected "oah_news_form_type_image", given "oah_newsbundle_image"

我的图像类型:

class ImageType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('file', 'file');
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'OAH\NewsBundle\Entity\Image'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'oah_newsbundle_image';
    }
}

谢谢!

【问题讨论】:

  • 你使用哪个版本的 symfony? FormType 中有 getName() 方法吗?
  • 我有 2.6.12 版本的 symfony。我编辑了我的第一篇文章以添加我的 ImageType

标签: php forms symfony service


【解决方案1】:

看来您的别名必须与来自getName()return 匹配。

【讨论】:

    【解决方案2】:

    @Flushdrew 在我发表评论后您是否设法解决了您的问题?无论如何,我会在这里给出完整的答案。我问了你使用的 symfony 版本,因为 getName 方法在 3.0 中被移除了,FormType 可以通过它的静态类名来识别。

    在 2.7 及更低版本的 FormType 类中,您必须提供 getName“announce”方法,该方法将通知其他 FormTypes 此特定表单使用此特定名称。

    /**
     * @return string
     */
    public function getName()
    {
        return 'oah_news_form_type_image';
    }
    

    实际上services.yml中的alias: 'oah_news_form_type_image'必须匹配getName()中的那个

    您可以在此处阅读有关它的更多信息: http://symfony.com/doc/2.7/cookbook/form/create_custom_field_type.html

    【讨论】:

    • 是的,对不起,但它工作得很好。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2016-11-27
    • 2018-10-07
    • 1970-01-01
    相关资源
    最近更新 更多