Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProfileForm as service not being created properly #104

Open
clytemnestra opened this issue Mar 8, 2016 · 2 comments
Open

ProfileForm as service not being created properly #104

clytemnestra opened this issue Mar 8, 2016 · 2 comments

Comments

@clytemnestra
Copy link

Everything works fine if the forms are not defined as services and need something in their constructors. However, in my ProfileController:

  $form = $this->get('fos_user.profile.form.factory')->->createForm();

calls

$type = $this->userDiscriminator->getFormType($this->type);

which calls

        $type = new $className($class);

This works for normal forms, but not for service forms. It tries to call

new MedAppBundle\Form\ProfileType("MedAppBundle\Entity\User") 

in my case, which turns into the error

Catchable Fatal Error: Argument 1 passed to MedAppBundle\Form\ProfileType::__construct() must be an instance of Application\Liip\ThemeBundle\Services\ThemeChanger, string given, called in E:\svn\medapp\vendor\pugx\multi-user-bundle\PUGX\MultiUserBundle\Model\UserDiscriminator.php on line 155 and defined

because my ProfilyType form defined as a service needs an object in its constructor

/**
 * @Service("app_user_profile_type")
 * @Tag("form.name", attributes={"alias"="app_user_profile_type"})
 */
class ProfileType extends AbstractType
{
    /**
     * @var ThemeChanger
     */
    private $changer;


    /**
     * @InjectParams({
     *     "changer" = @Inject("liip_theme.active_theme_changer")
     * })
     */
    public function __construct(ThemeChanger $changer)
    {
        $this->changer = $changer;
    }

Is this a not covered feature of this bundle or am I doing something wrong?

@clytemnestra clytemnestra changed the title Form as service not being created properly ProfileForm as service not being created properly Mar 8, 2016
@garak
Copy link
Member

garak commented Mar 8, 2016

This is currently not covered.
You can override pugx_user.manager.user_discriminator service writing a compiler pass, then extend our UserDiscriminator and override getFormType method. You'll likely need also to override constructor to inject your form service.

@clytemnestra
Copy link
Author

Uh, that was what I was trying to avoid. Hopefully it will be supported in the future, often forms are used as services.

The constructor needs to inject any form's own services, I can't really hardcode it, though. Even thought the discriminator is used only for register/profile forms.

In the meantime, I've just changed the way I create the form in the controller: $form = $this->get('form.factory')->create($this->get('app_user_profile_type')); where app_user_profile_type is the form service.

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

No branches or pull requests

2 participants