I would like to be able to inject a repository into a registration form (similar to this approach) to use in an event listener but I get:
Catchable Fatal Error: Argument 1 passed to AppBundle\Form\RegistrationType::__construct() must > be an instance of AppBundle\Entity\FocusRepository, string given, called in G:\Documents\workspace> \match\vendor\pugx\multi-user-bundle\PUGX\MultiUserBundle\Model\UserDiscriminator.php on line 155
I can work around this in a template but using an event listener seemed a more elegant solution. Any suggestions?
namespace AppBundle\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use FOS\UserBundle\Form\Type\RegistrationFormType;
use AppBundle\Entity\FocusRepository;
class RegistrationType extends RegistrationFormType
{
private $focusRepo;
public function __construct(FocusRepository $focusRepo)
{
$this->focusRepo = $focusRepo;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
...
}
...
}
services:
app.focus_repository:
class: Doctrine\ORM\EntityRepository
factory_service: doctrine.orm.default_entity_manager
factory_method: getRepository
arguments:
- AppBundle\Entity\Focus
The error occurs because PUGXMultiUserBundle uses FOSUserBundle registration (and profile) forms. Those forms already have a string (indicating the class of User) in the constructor. I've abandoned this question and am using a different approach. But I now know that I perhaps could have used a Compiler Pass to override the FOSUserBundle registration form service to allow an additional parameter. So a solution existed but was never tried.