【问题标题】:Customization of user profile form自定义用户资料表格
【发布时间】:2016-07-20 20:18:27
【问题描述】:

我想在 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".

谢谢你帮助我

【问题讨论】:

  • 为什么要使用服务?在控制器中创建一个表单和您自己的路由来处理数据和更新用户。
  • @MohammadGholami 他想自定义 FOSUserBundle 的表单,所以他必须使用服务
  • 我这样做是为了 FOSUserBundle!
  • 你为什么要修改控制器?它应该只适用于新表单。

标签: php forms symfony fosuserbundle


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2023-04-10
    相关资源
    最近更新 更多