Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - The type name specified for the service does not match

i try to configure EasyAdminBundle with my own ImageType to upload file in my ArticleType. So i created this service in 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' } ]

and tried to call it in my 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}

but i've the following error and i don't know how to fix it :

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"

my ImageType :

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

Thank you !

like image 253
Flushdrew Avatar asked Dec 25 '22 11:12

Flushdrew


2 Answers

It appears your alias name must match your return from getName().

like image 168
craigh Avatar answered Dec 27 '22 21:12

craigh


@Flushdrew Have you managed to fix your issue after my comment? Anyway I'll give complete answer here. I asked about symfony version you use, because getName method was removed in 3.0 and the FormType can be identified by its static class name.

In the 2.7 and below in the FormType class you have to provide a getName "announce" method, that will inform other FormTypes that this particular form uses this particular name.

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

Actually alias: 'oah_news_form_type_image' in the services.yml has to match the one in the getName()

You can read more about it here: http://symfony.com/doc/2.7/cookbook/form/create_custom_field_type.html

like image 39
rsobon Avatar answered Dec 27 '22 20:12

rsobon