Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Not able to decorate a form type service #290

Closed
jhonatanTeixeira opened this issue Jun 2, 2016 · 5 comments
Closed

Not able to decorate a form type service #290

jhonatanTeixeira opened this issue Jun 2, 2016 · 5 comments

Comments

@jhonatanTeixeira
Copy link

jhonatanTeixeira commented Jun 2, 2016

I needed to add a field to the registration form on sonata user bundle, and so i decided to decorate the service using the symfony service decoration feature like this:

<service id="sonata.user.registration.form.type.decorator" decorates="sonata.user.registration.form.type" class="Application\Sonata\UserBundle\Form\RegistrationFormDecorator" public="false">
            <argument type="service" id="sonata.user.registration.form.type.decorator.inner" />
</service>

but then i got this error:

The type name specified for the service "sonata.user.registration.form.type" does not match the actual name. Expected "Sonata\UserBundle\Form\Type\RegistrationFormType", given "Application\Sonata\UserBundle\Form\RegistrationFormDecorator"

looking into the code i got these lines in vendor/sonata-project/core-bundle/Form/Extension/DependencyInjectionExtension.php at line 103

if ($name !== get_class($type) && (method_exists($type, 'getName') && $type->getName() !== $name)) {
            throw new InvalidArgumentException(
                sprintf('The type name specified for the service "%s" does not match the actual name. Expected "%s", given "%s"',
                    $this->typeServiceIds[$name],
                    $name,
                    get_class($type)
                ));
        }

by comenting these lines i got my form working, i dont know why to remove the symfony decoration mechanism with this code, i don't know what kind of bug this verification covers, but i love the decorator pattern and so i love this symfony feature, is there any way to remove or refactor this code to fix this issue?

I love this project by the way, it saves my life always.

By the way, this is my environment:
PHP 7.0.7
Symfony 2.8.6
Sonata Core Bundle 3.0.1
Sonata Admin Bundle 3.1.0

@greg0ire
Copy link
Contributor

greg0ire commented Jun 2, 2016

Can you find the commit that introduced that piece of code? Maybe there is a commit message that explains this ?

@jhonatanTeixeira
Copy link
Author

jhonatanTeixeira commented Jun 2, 2016

commit 0bd414f fix deprecated notices by raibax.
commit af706a9 fix test with sf3 also by raibax.

seens to be a important fix, still don't understand much, is there any other way for me to customize the registration form? I tried to extend the class and them aliasing the service, but them this same container didn't inject the dependencies.

heres the config:

        <service id="sonata.user.registration.form.type" class="Application\Sonata\UserBundle\Form\RegistrationFormType">
            <tag name="form.type" alias="sonata_user_registration" />
            <argument>%fos_user.model.user.class%</argument>
            <argument>%sonata.user.registration.form.options%</argument>
        </service>

heres the error:

Warning: Missing argument 1 for Sonata\UserBundle\Form\Type\RegistrationFormType::__construct(), called in /home/jhon/sonata_crm/vendor/sonata-project/core-bundle/Form/Extension/DependencyInjectionExtension.php on line 94 and defined

my code is simple as this:

use Sonata\UserBundle\Form\Type\RegistrationFormType as BaseRegitrationFormType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationFormType extends BaseRegitrationFormType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder->add('company');
    }
}

any sugestions?

Btw, this extended class was working on Sonata Core Bundle 2.4@dev

@jhonatanTeixeira
Copy link
Author

I have just solved my issue by doing the following:

config.yml

sonata_user:
    profile:
        register:
            form:
                type:                 sonata_user_registration_company
                handler:              sonata.user.registration.form.handler.default
                name:                 sonata_user_registration_form

service definition:

        <service id="sonata.user.registration.form.type.extension" class="Application\Sonata\UserBundle\Form\RegistrationFormType">
            <tag name="form.type" alias="sonata_user_registration_company" />
            <argument>%fos_user.model.user.class%</argument>
            <argument>%sonata.user.registration.form.options%</argument>
        </service>

and my class:

namespace Application\Sonata\UserBundle\Form;

use Sonata\UserBundle\Form\Type\RegistrationFormType as BaseRegitrationFormType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationFormType extends BaseRegitrationFormType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder->add('company');
    }

   //i had to override this method so i could change its alias on form.type tag
    public function getBlockPrefix()
    {
        return 'sonata_user_registration_company';
    }
}

I guess that on my first try i got confused by the service aliasing and the form.type tag aliasing.

Even tho i've solved my problem, i still couldn't use the service decoration wich is a elegant solution for this situation where one has to add fields into a existing form type.

Thanks.

@greg0ire
Copy link
Contributor

greg0ire commented Jun 2, 2016

I think you might be right. If you find a solution that does not trigger a deprecation warning and works with sf3, a PR will be very welcome!

@jordisala1991
Copy link
Member

The class you are having problems with, got deprecated since this PR: #462

You can follow the docs to opt-out of using it.

Closing because this is fixed with a workaround and with the correct way (disabling core form mapping)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants