0

我正在使用 FOSUserBundle 来管理我的用户,并且我正在尝试覆盖配置文件编辑表单,遵循此文档指南https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md 这是我的表格类型:

<?php


namespace Tracker\UserBundle\Form\Type;

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

class ProfileFormType extends BaseType
{


    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $this->buildUserForm($builder, $options);

    }


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


    /**
     * Builds the embedded form representing the user.
     *
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    protected function buildUserForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
            ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
            ->add('avatar','file',array( 'required'=>'true'));
    }
}

这是我的services.yml补充:

 tracker_user.profile.form.type:
         class: Tracker\UserBundle\Form\Type\ProfileFormType
         arguments: [%fos_user.model.user.class%]
         tags:
             - { name: form.type, alias: tracker_user_profile }

这是config.yml设置的部分FOSUSerBundle

profile:
    form:
        type: tracker_user_profile

最后是我的控制器动作,我几乎从原始 FOSUser 控制器中复制了 1to1:

/**
 * Edit the user
 */
public function editAction()
{
    $user = $this->container->get('security.context')->getToken()->getUser();
    if (!is_object($user) || !$user instanceof UserInterface) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    $form = $this->container->get('tracker_user.profile.form.type');
    $formHandler = $this->container->get('fos_user.profile.form.handler');

    $process = $formHandler->process($user);
    if ($process) {
        $this->setFlash('fos_user_success', 'profile.flash.updated');

        return new RedirectResponse($this->getRedirectionUrl($user));
    }

    return $this->container->get('templating')->renderResponse(
        'FOSUserBundle:Profile:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
        array('form' => $form->createView())
    );
}

当我调用页面时,我收到错误:

致命错误:在第 105 行的 /coding/src/Tracker/UserBundle/Controller/ProfileController.php 中调用未定义的方法 Tracker\UserBundle\Form\Type\ProfileFormType::createView()

我设置服务的方式有什么问题吗?还是我的代码?

4

1 回答 1

1

在您的控制器中,检索 Form 实例而不是 FormType 实例:

$form = $this->container->get('fos_user.profile.form');
于 2012-09-16T11:18:14.397 回答