我正在使用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
最后是我的Controller Action,我几乎从原来的FOSUser Controller复制了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()
我设置服务的方式有什么问题吗?还是我的代码?
答案 0 :(得分:1)
在您的控制器中,检索Form实例而不是FormType实例:
$form = $this->container->get('fos_user.profile.form');