Titus
Titus

Reputation: 101

Symfony2 FOSUserBundle override profile form : field form empty?

I overrided registration form from FOSUserBundle with additionals fields: it works well.

When I apply the same logic to override Profile Form : the form appears well with my additionals fields but all is empty (the fields do not contain values ​​of the connected user).

Note: when I use the default form from the bundle the profile form contains the values ​​of the connected user.

Is there a specific action compared to override the registration form to retrieve the values ​​of the connected user ?

HERE IS CODE :

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',

        );
    }

}

Upvotes: 3

Views: 10912

Answers (1)

Titus
Titus

Reputation: 101

I found the mistake in my file ProfilFormeHandler.php : in the function process() I called parent::onSucess() instead of parent::process() ... The result is a "silent" bug (silent because not fatal error appears) due to my fault of course

Thanks for time you spent to try to help me, very sorry !

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

Upvotes: 5

Related Questions