Symfony2 FOSUserBundle覆盖配置文件表单:字段表空?

时间:2012-06-29 23:48:26

标签: symfony override profile fosuserbundle

我将FOSUserBundle的注册表格覆盖了附加字段:它运作良好。

当我应用相同的逻辑来覆盖配置文件表单时:表单与我的附加字段一起显示,但所有字段都是空的(字段不包含已连接用户的值)。

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

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

这是代码:

的src / VN / UserBundle /资源/配置/ 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 /应用/配置/ config.yml

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

的src / VN / UserBundle /窗体/类型/ 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 /窗体/类型/ 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',

        );
    }

}

1 个答案:

答案 0 :(得分:5)

我在我的文件ProfilFormeHandler.php中发现了错误:在函数process()中我调用了parent :: onSucess()而不是parent :: process()... 结果是一个“沉默”的错误(因为我的错误当然没有出现致命的错误而沉默)

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

  <?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);
      }
  }
相关问题