自定义用户配置文件表单

时间:2016-07-20 20:18:27

标签: php forms symfony fosuserbundle

我想在symfony2中自定义用户个人资料表单。我做了我所知道的但是没有用。 这是代码

service.yml

services
     meublestunis_user.form.profile:
          class: UserBundle\Form\UserProfileFormType
          arguments: [%fos_user.model.user.class%]
          tags:
               - { name: form.type, alias: edit_user_profile} 

config.yml

fos_user:
     profile:
          form:
               type: edit_user_profile 

UserProfileForm.php

<?php

namespace UserBundle\Form;

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

class UserProfileFormType extends BaseType {

    public function buildUserForm(FormBuilderInterface $builder, array $options) {
        parent::buildUserForm($builder, $options);
        $builder
                ->add('nom', null)
                ->add('prenom', null)
                ->add('adresse', null)
                ->add('sexe', null)
                ->add('date_naissance', null)
                ->add('ville', null)
                ->add('mobile', null)
                ->add('site_web', null)
        ;
    }


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

    public function getParent()
    {
        return 'fos_user_profile';
    }

}

控制器

//modification part
        if (!is_object($user) || !$user instanceof UserInterface) {
            throw new AccessDeniedException('This user does not have access to this section.');
        }
        $dispatcher = $this->get('event_dispatcher');
        $event = new GetResponseUserEvent($user, $request);
        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
        if (null !== $event->getResponse()) {
            return $event->getResponse();
        }
        $formFactory = $this->get('meublestunis_user.form.profile');
        $editForm = $formFactory->createForm();
        $editForm->setData($user);
        $editForm->handleRequest($request);
        if ($editForm->isValid()) {
            /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
            $userManager = $this->get('fos_user.user_manager');
            $event = new FormEvent($editForm, $request);
            $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
            $userManager->updateUser($user);
            if (null === $response = $event->getResponse()) {
                $url = $this->generateUrl('show_user_profile');
                $response = new RedirectResponse($url);
            }
            $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
            return $response;
        }

错误为Attempted to call an undefined method named "createForm" of class "UserBundle\Form\UserProfileFormType".

谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

您的UserBundle是否扩展了FosBundle?

namespace UserBundle;

class UserBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

在你的config.yml中定义你的新个人资料表格,如下所示:

fos_user:
    profile:
        form:
            type: UserBundle\Form\UserProfileFormType
            name: edit_user_profile
        validation_groups:  [Profile, Default]