Easyadminbundle: Cannot use custom form type as service (SF4 /w autowiring)

Created on 29 Nov 2018  路  7Comments  路  Source: EasyCorp/EasyAdminBundle

If I create a new form type that has a service in the constructor, I can not use it in the panel.

Error:

Too few arguments to function App\Form\RouteType::__construct(), 0 passed in ./vendor/symfony/form/FormRegistry.php on line 92 and exactly 1 expected

RouteType.php:

class RouteType extends AbstractType
{
    /**
     * @param RouterInterface $router
     */
    public function __construct(RouterInterface $router)
    {
    }
}

Config:

easy_admin:
    entities:
        MyEntity:
            form:
                fields:
                    - {property: "route", label: "Route", type: \App\Form\RouteType}

bug confirmed

Most helpful comment

Does it make a difference if you omit the leading backslash (i.e. use App\Form\RouteType instead of \App\Form\RouteType)?

All 7 comments

Does it make a difference if you omit the leading backslash (i.e. use App\Form\RouteType instead of \App\Form\RouteType)?

I confirm that removing the leading backslash solved the problem. This is confusing because just below I have a different custom form type that works with a leading backslash. I suspect that this is because it is not a service:

form:
    fields:
        - {property: "route", label: "Trasa", type: App\Form\RouteType}
        - {property: "title", label: "Tytu艂"}
        - {property: "published", label: "Opublikowany"}
        - {property: "seo", label: false, type: \App\Form\SeoType}

I think it would be useful to put in the documentation information about the leading backslash.

@gander Is App\Form\RouteType registered as a service and autoconfigured?

This error is not related to this bundle and is expected even in PHP context: $formBuilder->add('route', '\App\Form\RouteType') it'll fails too because the service id is App\Form\RouteType and it doesn't match, hence the form system tries to initialize the form type (new $type()) but the class requires a constructor argument.

I think we can close here.

@gander Is App\Form\RouteType registered as a service and autoconfigured?

Yes. This is a correctly registered and configured service.

\App\Form\SeoType it's just an element, not a service, that's why it works without a problem.

And it is misleading.

Closing as it's not an EasyAdmin bug, thanks!

I think we should still deprecate the ability to configure the form type with a leading backslash. There is no real value in it, but much potential for confusion.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haithem-rihane picture haithem-rihane  路  4Comments

BigMichi1 picture BigMichi1  路  3Comments

Wait4Code picture Wait4Code  路  3Comments

javiereguiluz picture javiereguiluz  路  4Comments

seb-jean picture seb-jean  路  3Comments