symfony 2.0 FOSUserBundle – overriding form produces “Could not load type” error

If you are using FOSUserBundle with symfony 2.0 and you need to override any of the forms of proceeding “included” with this plugin
then, without a doubt, you’ve read this note: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md
After updating your page can happen unfortunate situation – the error message “Could not load type ….” (dots here will be the alias which you gave to your custom form type).
It seems to be in the file app/config.yml all right with the paddings (meaning configuration of the fos_user service), and a configuration file for your service service.xml (service.yml) (in CustomUserBundle) is described correctly, and everywhere the same alias, getName () and so on … but still the same error. Doesn’t it? It does not matter! 🙂 There is a solution to your problem! Read on, and everything will be easy!

To understand and properly use the services and dependency injection in symfony 2.x, highly recommend reading this and this. The bottom line is that in order to see your symfony framework services and extensions, you must provide information about the configuration of your bundle.
You can do this in two ways.

1. The standard configuration of the service.
You can specify your services in a configuration file (e.g. services.yml) that lives in your bundle and then import it from your main application configuration. This is really easy, quick and totally effective. If you make use of parameters, then you still have the flexibility to customize your bundle from your application configuration. See “Importing Configuration with imports” for more details.
Example:

imports:
- {Resource: parameters.ini}
- {Resource: security.yml}
- {Resource: @CustomUserBundle/Resources/config/services.xml}

2. The semantic description of the configuration.
This is the way configuration is done with the core bundles (as described above). The basic idea is that, instead of having the user override individual parameters, you let the user configure just a few, specifically created options. As the bundle developer, you then parse through that configuration and load services inside an “Extension” class. With this method, you won’t need to import any configuration resources from your main application configuration: the Extension class can handle all of this.
The second method is described in details in the second of the links above.

So, returning to the nature of our error. The point is this: it may happen that the “Custom” bundle for the user (User) you have created manually (by copying from existing Bandle all necessary files and directories) rather than using a console command Symphony (generate:bundle).
In this case, you might have forgotten to create the directory DependencyInjection and CustomUserExtension.php and Configuration.php files uder it.
Or, if you did not intend to use semantic configuration, you jsut forgot to import your CustomUser service’s configuration file into your main config file.

That’s it. Now you know about the two possible solutions to this problem. Then it’s up to you which way to choose – simple (through imports) or flexible (through the extenstion and semantic configuration).

3 Replies to “symfony 2.0 FOSUserBundle – overriding form produces “Could not load type” error”

  1. In some words: thank you ! You gave me the right solution in using the semantic configuration, which is more flexible for my custom fosuser bundle. You spare me hours of research !

Leave a Reply

Your email address will not be published. Required fields are marked *