簡體   English   中英

定制用戶個人資料表格

[英]Customization of user profile form

我想在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} 

配置文件

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".

感謝你們對我的幫助

您的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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM