Symfony2 FOSUserBundle 覆盖配置文件形式 : field form empty?

标签 symfony overriding profile fosuserbundle

我用附加字段覆盖了 FOSUserBundle 的注册表单:效果很好。

当我应用相同的逻辑覆盖配置文件表单时:该表单与我的附加字段一起显示良好,但全部为空(字段不包含连接用户的值)。

注意:当我使用 bundle 中的默认表单时,配置文件表单包含已连接用户的值。

是否有特定的操作可以覆盖注册表单以检索连接用户的值?

这里是代码:

src/Vn/UserBundle/Resources/config/services.yml

services:
 ...
  vn_user.profile.form.type:
    class: Vn\UserBundle\Form\Type\ProfileFormType
    arguments: [%fos_user.model.user.class%]
    tags:
        - { name: form.type, alias: vn_user_profile }

  vn_user.form.handler.profile:
    class: Vn\UserBundle\Form\Handler\ProfileFormHandler
    arguments: ["@fos_user.profile.form", "@request", "@fos_user.user_manager"]
    scope: request
    public: false 

symfony/app/config/config.yml

  fos_user:
  ...
  profile:
        form:
              type: vn_user_profile
              handler: vn_user.form.handler.profile

src/Vn/UserBundle/Form/Type/ProfileFormType.php

  namespace Vn\UserBundle\Form\Type;

  use Symfony\Component\Form\FormBuilder;
  use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;

  class ProfileFormType extends BaseType
  {    
      public function buildUserForm(FormBuilder $builder, array $options)
      {        
      parent::buildUserForm($builder, $options);

          // custom field       
          $builder->add('profile',new MyProfileFormType(),array(
                    'label' => 'PROFILE'
                    ));

      }

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

src/Vn/UserBundle/Form/Type/MyProfileFormType.php

namespace Vn\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class MyProfileFormType extends AbstractType
{

    public function buildForm(FormBuilder $builder, array $options)
    {
    $builder->add('birthday','birthday', array(
        'input' => 'array',
        'widget' => 'choice',

        'label'  => 'Birthday',
        ))
        ->add('firstname','text', array(
        'trim' => true,
        'label'  => 'Firstname',
        ))
        ->add('lastname','text', array(
        'trim' => true,
        'label'  => 'Lastname',
        ))
        ->add('gender','choice', array(
        'choices'   => array('1' => 'Male', '2' => 'Female'),
        'expanded'  => true,
        'required'  => true,
        'label'  => 'Vous êtes',
        ));
    }

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


    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Vn\UserBundle\Document\Profile',

        );
    }

}

最佳答案

我在文件 ProfilFormeHandler.php 中发现了错误:在函数 process() 中,我调用了 Parent::onSucess() 而不是 Parent::process() ... 结果是一个“无声”错误(无声是因为没有出现 fatal error )当然是我的错

感谢您花时间帮助我,非常抱歉!

  <?php
  // src/Vn/UserBundle/Form/Handler/RegistrationFormHandler.php

  namespace Vn\UserBundle\Form\Handler;

  use FOS\UserBundle\Form\Handler\ProfileFormHandler as BaseHandler;

  use FOS\UserBundle\Model\UserInterface;


  class ProfileFormHandler extends BaseHandler
  {

      public function process(UserInterface $user)
      {
          //parent::onSuccess($user);
            parent::process($user); // sound better of course : )



      }

      protected function onSuccess(UserInterface $user)
      {
          $this->userManager->updateUser($user);
      }
  }

关于Symfony2 FOSUserBundle 覆盖配置文件形式 : field form empty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11270282/

相关文章:

php - 使用 Doctrine 设置 "time"或 DateInterval

php - 在 Symfony 中注册特定的表单类型 - Compiler Pass

java - 覆盖 hashCode() 和 equals() 方法

python - python 类多重继承中的方法重写

php - symfony 表单事件订阅者 $event->getData() 返回 NULL

symfony - 将安全用户的名称传递给 Symfony2 中 services.yml 中的监听器

c++ - 我将如何通过抽象类传递虚拟方法但可选择不覆盖?

php - 如何检索旧的 Whatsapp 个人资料图片?

powershell - 如何在 PowerShell 中从脚本文件重新加载用户配置文件

ios - iOS 的 BT 4.0 LE 消费者文件