FOS UserBundle - Override FormFactory

I need help overriding FormFactory. My goal is to change my profile. Therefore, since I am also using Facebook Login, I do not want them to change their email address, username and password. So I am using ProfileController in my package to pass the current user to the ProfileFormType class.

What I am trying to do is implement my own FormFactory, so I can set the user and put it in a parameter array inside the call

return $this->formFactory->createNamed($this->name, $this->type, null, array('validation_groups' => $this->validationGroups, 'user' => $this->user));

      

To achieve this, I need to define my FormFactory in sevices.yml.

Here is the original from FOSUserBundle:

<service id="fos_user.profile.form.factory" class="FOS\UserBundle\Form\Factory\FormFactory">
        <argument type="service" id="form.factory" />
        <argument>%fos_user.profile.form.name%</argument>
        <argument>%fos_user.profile.form.type%</argument>
        <argument>%fos_user.profile.form.validation_groups%</argument>
    </service>

      

I am having a hard time translating this into yml as I do not fully understand the use of aliases.

Could you help me identify it correctly? Something like

skt_user.profile.form.factory:
    class: SKT\UserBundle\Form\Factory\FormFactory
    arguments: ???

      

+2


source to share


1 answer


Funny, after posting, I found a solution. This is the correct configuration for my FormFactory:

skt_user.profile.form.factory:
    class: SKT\UserBundle\Form\Factory\FormFactory
    arguments: ["@form.factory", "%fos_user.profile.form.name%", "%fos_user.profile.form.type%", "%fos_user.profile.form.validation_groups%"]

      

In my controller, I just used these two lines:

$formFactory = $this->container->get('skt_user.profile.form.factory');
$formFactory->setUser($user);

      

In the factory I have implemented this function



namespace SKT\UserBundle\Form\Factory;

use Symfony\Component\Form\FormFactoryInterface;
use FOS\UserBundle\Form\Factory\FactoryInterface;

class FormFactory implements FactoryInterface
{
  private $formFactory;
  private $name;
  private $type;
  private $validationGroups;
  private $user;

  public function __construct(FormFactoryInterface $formFactory, $name, $type, array $validationGroups = null)
  {
    $this->formFactory      = $formFactory;
    $this->name             = $name;
    $this->type             = $type;
    $this->validationGroups = $validationGroups;
  }

  public function createForm()
  {
    return $this->formFactory->createNamed($this->name, $this->type, null, array('validation_groups' => $this->validationGroups, 'user' => $this->user));
  }

  public function setUser($user)
  {
    $this->user = $user;
  }
}

      

and this is how my Formtype looks like

<?php

namespace SKT\UserBundle\Form\Type;

use SKT\CaromBundle\Repository\PlayerRepository;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ProfileFormType extends \FOS\UserBundle\Form\Type\ProfileFormType
{

  private $class;

  /**
   * @param string $class The User class name
   */
  public function __construct($class)
  {
    $this->class = $class;
  }


  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    // Do not show email and username if login uses facebook
    if (!$options['user']->getFacebookId()) {
    $builder
      ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
      ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'));
    }

    $builder
      ->add('firstname', null, array('label' => 'Vorname'))
      ->add('lastname', null, array('label' => 'Nachname'))
      ->add('player', 'entity', array(
        'label'         => 'Spieler',
        'class'         => 'SKTCaromBundle:Player',
        'property'      => 'name',
        'query_builder' => function (PlayerRepository $er) {
          return $er->createQueryBuilder('p')
            ->orderBy('p.name', 'ASC');
        },
        'empty_value'   => 'Verbinde Dich mit einem Spieler',
        'required'      => false,
      ));
  }

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


  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
    $resolver->setDefaults(array(
      'data_class' => $this->class,
      'intention'  => 'profile',
      'user' => null
    ));
  }
}

      

works great!

+2


source







All Articles