PUGX / PUGXMultiUserBundle

An extension for FOSUserBundle to handle users of different types. Compatible with Doctrine ORM.
163 stars 96 forks source link

ProfileForm as service not being created properly #104

Open clytemnestra opened 8 years ago

clytemnestra commented 8 years ago

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?

garak commented 8 years ago

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 commented 8 years ago

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.