PUGX / PUGXMultiUserBundle

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

FormType: InvalidConfigurationException: Unrecognized options "arguments, tags" #44

Closed michaelv closed 10 years ago

michaelv commented 10 years ago

Hi,

I recently started using the PUGXMultiUserBundle, but there doesn't seem to be the possibility to pass additional parameters to the constructor of RegistrationFormType. The reason to pass these parameters is because the form contains a 'country' dropdown field which needs to be localized. The countries come from a database, while the translations come from the translation files.

My RegistrationFormType looks like this:

class RegistrationFormType extends BaseType
{
    private $doctrine;
    private $translator;

    public function __construct($class, RegistryInterface $doctrine, $translator)
    {
        parent::__construct($class);
        $this->doctrine = $doctrine;
        $this->translator = $translator;
    }
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $countries = $repository = $this->doctrine->getRepository('AcmeBaseBundle:Country')->findAll();

        $dropdown_countries = array();

        foreach($countries as $country) {
            $dropdown_countries[$country->getCodeISOa2()] = $this->translator->trans('country_'.$country->getCodeISOa2());
        }
        $builder->add('country', 'choice', 
                    array( 
                    'attr' => array('class' => 'countriesDropdown'), 
                    'label' => 'registration_label_country', 
                    'choices' => $dropdown_countries));
}

config.yml:

pugx_multi_user:
  users:
    user_create_my_book:
        entity: 
          class: Acme\Bundle\UserBundle\Entity\MyCustomUser
        registration:
          form: 
            type: Acme\Bundle\UserBundle\Form\Type\RegistrationFormType
            arguments: [%fos_user.model.user.class%, @doctrine, @translator ]
            tags:
                - { name: form.type, alias: acme_user_registration_form }
            name: acme_custom_user_registration
            validation_groups:  [Registration, Default]
          template: FOSUserBundle:Registration:register.html.twig

Acme/Bundle/UserBundle/Resources/config/services.xml :

<service id="acme_user.registration.form.type" class="Acme\Bundle\UserBundle\Form\Type\RegistrationFormType">
            <tag name="form.type" alias="acme_custom_user_registration" />
            <argument>%fos_user.model.user.class%</argument>
            <argument type="service" id="doctrine" />
            <argument type="service" id="translator"></argument>
        </service>

I get this error:

InvalidConfigurationException: Unrecognized options "arguments, tags"

If I leave out this part from config.yml:

            arguments: [%fos_user.model.user.class%, @doctrine, @translator ]
            tags:
                - { name: form.type, alias: acme_user_registration_form }

I get this error:

Catchable Fatal Error: Argument 2 passed to Acme\Bundle\UserBundle\Form\Type\RegistrationFormType::__construct() must implement interface Symfony\Bridge\Doctrine\RegistryInterface, none given, called in vendor/pugx/multi-user-bundle/PUGX/MultiUserBundle/Model/UserDiscriminator.php on line 155 and defined in src/Acme/Bundle/UserBundle/Form/Type/RegistrationFormType.php line 14 

Could you please help me out with this?

Thanks alot!

Kr, Michael

leopro commented 10 years ago

the bundle only wraps fosub's behaviour, see here: https://github.com/PUGX/PUGXMultiUserBundle/blob/master/DependencyInjection/Compiler/OverrideServiceCompilerPass.php

so, you can do a similar thing in your project, overriding pugx form factories.

anyway, for a country dropdown field you can use a country field type http://symfony.com/doc/current/reference/forms/types/country.html

michaelv commented 10 years ago

Hi leopro,

Thank you very much for the quick reply!

lplume commented 10 years ago

hello,

@michaelv did you try to override pugx form factories?

@leopro i am trying to do so, but isn't the user discriminator model (pugx_user.manager.user_discriminator service) that instantiate the form type ( https://github.com/PUGX/PUGXMultiUserBundle/blob/master/Model/UserDiscriminator.php#L146 )?

So shouldn't be the user discriminator overrided as well? [edit] in order to pass extra args to the form type? [/edit]