FOS UserBundle - Override the FormFactory

ぃ、小莉子 提交于 2019-12-13 16:23:07

问题


i need some help overriding the FormFactory. My Goal is to change the Profile. So, as i also use Facebook Login, i do not want them to change email, username and password. So i use the ProfileController in my bundle to hand over the current user to the ProfileFormType class.

What i'm trying to do is to implement my own FormFactory, so i can set the user and put it into the options 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 one 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 have difficulties to translate this into yml, as i do not understand the usages of aliases completely.

Could you help me to define it correct? Something like

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

回答1:


Funny, after posting it, I found the 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 simply used these 2 lines:

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

In the factory, I 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

<?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 perfect!



来源:https://stackoverflow.com/questions/19036237/fos-userbundle-override-the-formfactory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!