rollerworks-graveyard / RollerworksMultiUserBundle

Multi user management for the FOSUserBundle - DISCONTINUED!!
MIT License
56 stars 21 forks source link

Additional arguments for forms type #66

Closed slowprog closed 10 years ago

slowprog commented 10 years ago

Hello, I'm trying override registration form. And I need some services (@translator or all them from @service_container), but i can't send it.

With FOSUserBundle I created form.type with additional arguments:

acme.registration.form.type:
    class: Acme\OtherBundle\Form\Entity\CustomerRegistrationType
    arguments: [%fos_user.model.user.class%, "@translator"]
    tags:
        - { name: form.type, alias: acme_registration }

But with RollerworksMultiUserBundle I have fatal error:

Catchable Fatal Error: Argument 2 passed to CustomerRegistrationType::__construct() must be an instance of Symfony\Component\Translation\Translator, none given...

Can you help me, please?

sstok commented 10 years ago

What does your Extension class look like? It seems the acme_registration form-type is overwritten.

In this example, try removing the class so it will not overwrite your type.

'form' => array(
                        'class' => 'FOS\\UserBundle\\Form\\Type\\RegistrationFormType',
                        'type' => 'fos_user_registration',
                        'name' => 'fos_user_registration_form',
                        'validation_groups' => array('Registration', 'Default'),
                    ),
slowprog commented 10 years ago

This is my Extension class:

use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;

class CustomerRegistrationType extends BaseType 
{
    private $translator;
    private $router;
    public function __construct($class, Translator $translator, Router $router) 
    {
        parent::__construct($class);
        $this->translator = $translator;
        $this->router = $router;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder->add('limitation', null, array(
            'help' => $this->translator->trans('help.registration.limitation', array(
                '%link_service%' => $this->router->generate('text_page_service'),
            ))
        ));
    }

    public function getName()
    {
        return 'acme_registration';
    }
}

And then I configure the user-class:

'form' => array(
    'class' => 'Acme\\OtherBundle\\Form\\Entity\\CustomerRegistrationType',
    'type' => 'acme_registration',
    'name' => 'acme_registration_form',
    'validation_groups' => array('Registration', 'Default'),
),

The error occurs because the __construct is waiting Translator and Router but not gets their. Without Translator and Router working fine, but I need them.

How can I send their to the __construct?

sstok commented 10 years ago

The 'class' parameter is only used for fast registering (it will register the service for you), if you're form-type has external dependencies you need to register the form-type as service (as you did above) and remove the class from the Extension class.

'form' => array(
    'type' => 'acme_registration',
    'name' => 'acme_registration_form',
    'validation_groups' => array('Registration', 'Default'),
),

And this is also documented :) https://github.com/rollerworks/RollerworksMultiUserBundle/blob/master/docs/overriding_forms.md#automatic-registering

But not completely clear, I'll update this to make it more clear that you don't need the 'class' option when you're using your registering the form-type service you're self.

slowprog commented 10 years ago

Yeah, I'm tried this. I removed "class", setted "type" and "name", but rendered form of FOSUserBundle by default with 4 fields (email, username, plainPassword and _token) without limitation. Not acme_registration but acme_customer_register (name of the user-system is acme_customer).

I don't understand. Maybe I'm missing something?

slowprog commented 10 years ago

I got it! I ignored the following phrase:

Remember form types and names start with the service-prefix of the user-system.

I changed name of service acme.registration.form.type to the acme_customer.registration.form.type and it's work fine!

Sorry. My fault.