PUGX / PUGXMultiUserBundle

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

A method to add options to registration, profile forms #93

Open truckee opened 8 years ago

truckee commented 8 years ago

In this fork there is a method to add an array of options to both registration and profile forms. Testing the method is, however, currently outside of my skill set. I'd appreciate some pointers. Functionally these modifications satisfy the requirement of adding options to templates.

An example of how the method is used in a form:


        $builder->addEventListener(FormEvents::PRE_SET_DATA,
            function (FormEvent $event) {
            $form = $event->getForm();
            if ($this->options['skill_required']) {
                $form->add('skills', 'skills');
            };
            if ($this->options['focus_required']) {
                $form->add('focuses', 'focuses');
            };
        });

Here's an outline of the method:

pugx_multi_user:
  users:
    volunteer:
        options: 
            skill_required: true
            focus_required: false
        $rootNode->
                children()
                    ->arrayNode('users')->prototype('array')
                        ...
                        ->children()
                            ->arrayNode('options')
                                ->prototype('scalar')->defaultValue(null)->end()
                            ->end()
                        ->end()

    pugx_multi_user.registration.form.type:
        class: PUGX\MultiUserBundle\Form\RegistrationFormType
        arguments: [%fos_user.model.user.class%]
        tags:
            - { name: form.type, alias: fos_user_registration }        

    pugx_multi_user.profile.form.type:
        class: PUGX\MultiUserBundle\Form\ProfileFormType
        arguments: [%fos_user.model.user.class%]
        tags:
            - { name: form.type, alias: fos_user_profile }        
        $this->changeService(
                $container,
                'fos_user.registration.form.type',
                'pugx_multi_user.registration.form.type');

        $this->changeService(
                $container,
                'fos_user.profile.form.type',
                'pugx_multi_user.profile.form.type');
protected function buildConfig(array $users)
    {
        ...
            $this->conf[$class] = array(
                    ...
                    'options' => $user['options']
                );
        }

and

    public function getFormType($name)
    {
        $class = $this->getClass();
        $className = $this->conf[$class][$name]['form']['type'];
        $options = $this->conf[$class]['options'];    //added

        if (!class_exists($className)) {
            throw new \InvalidArgumentException(sprintf('UserDiscriminator, error getting form type : "%s" not found', $className));
        }

        $type = new $className($class, $options);    //parameter added
        return $type;
    }
    private $class;
    protected $options;

    /**
     * @param string $class The User class name
     */
    public function __construct($class, $options = null)
    {
        $this->class = $class;
        $this->options = $options;
    }
truckee commented 8 years ago

I've sorted out my testing concerns, both application's functional testis and the bundles unit tests. A pull request has been made.