FOS UserBundle-覆盖FormFactory [英] FOS UserBundle - Override the FormFactory

查看:69
本文介绍了FOS UserBundle-覆盖FormFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些覆盖FormFactory的帮助. 我的目标是更改配置文件.因此,因为我也使用Facebook登录名,所以我不希望他们更改电子邮件,用户名和密码. 因此,我在捆绑软件中使用ProfileController将当前用户移交给ProfileFormType类.

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.

我想做的是实现自己的FormFactory,因此我可以设置用户并将其放入调用内的options数组中

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));

要实现此目的,我需要在sevices.yml中定义我的FormFactory.

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

这是来自FOSUserBundle的原始照片:

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>

由于我不完全了解别名的用法,因此很难将其转换为yml.

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: ???

推荐答案

有趣,在将其发布后,我找到了解决方案.这是我的FormFactory的正确配置:

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;
  }
}

这就是我的Formtype的样子

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
    ));
  }
}

工作完美!

这篇关于FOS UserBundle-覆盖FormFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆