Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
truckee opened this issue Oct 26, 2015 · 1 comment
Open

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

truckee opened this issue Oct 26, 2015 · 1 comment

Comments

@truckee
Copy link

truckee commented Oct 26, 2015

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:

  • Add options: to pugx_multi_user: user:. Example:
pugx_multi_user:
  users:
    volunteer:
        options: 
            skill_required: true
            focus_required: false
  • Add options parameter to PUGX\MultiUserBundle\DependencyInjection\Configuration:
        $rootNode->
                children()
                    ->arrayNode('users')->prototype('array')
                        ...
                        ->children()
                            ->arrayNode('options')
                                ->prototype('scalar')->defaultValue(null)->end()
                            ->end()
                        ->end()
  • add services:

    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 }        
  • Incorporate services in PUGX\MultiUserBundle\DependencyInjection\Compiler\OverrideServiceCompilerPass:
        $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');
  • modify PUGX\MultiUserBundle\Model\UserDiscriminator:
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;
    }
  • copy in FOSUserBundle Registration & Profile forms and modify each:
    private $class;
    protected $options;

    /**
     * @param string $class The User class name
     */
    public function __construct($class, $options = null)
    {
        $this->class = $class;
        $this->options = $options;
    }
@truckee
Copy link
Author

truckee commented Oct 28, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant