我正在尝试覆盖SonataUserBundle的ProfileFormType。 我在该表单中添加了一些额外的字段,并在页面上呈现了所有字段。这样才行。但现在我想知道为什么用户的数据没有加载,因为firstname,lastname,...已经知道但只是没有显示在表单的文本框中。
重写的ProfileController类的editProfileAction:
/**
* @return Response
*
* @throws AccessDeniedException
*/
public function editProfileAction()
{
$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('sonata.user.profile.form');
$formHandler = $this->container->get('sonata.user.profile.form.handler');
$process = $formHandler->process($user);
if ($process) {
$this->setFlash('fos_user_success', 'profile.flash.updated');
return new RedirectResponse($this->generateUrl('sonata_user_profile_show'));
}
// This doesn't show the firstname
die($form->getData()->getFirstname());
return $this->render('SonataUserBundle:Profile:edit_profile.html.twig', array(
'form' => $form->createView(),
'theme' => $this->container->getParameter('fos_user.template.theme')
));
}
重写的ProfileFormHandler类的进程函数:
public function process(UserInterface $user)
{
$this->form->setData($user);
// This DOES show the firstname
die($this->form->getData()->getFirstname());
if ('POST' == $this->request->getMethod()) {
$this->form->bindRequest($this->request);
if ($this->form->isValid()) {
$user->upload();
$this->onSuccess($user);
return true;
}
// Reloads the user to reset its username. This is needed when the
// username or password have been changed to avoid issues with the
// security layer.
$this->userManager->reloadUser($user);
}
return false;
}
Services.yml:
services:
application_sonata_user.registration.form.type:
class: Application\Sonata\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: application_sonata_user_registration }
application_sonata_user.profile.form.type:
class: Application\Sonata\UserBundle\Form\Type\ProfileType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: application_sonata_user_profile }
application_sonata_user.search.form.type:
class: Application\Sonata\UserBundle\Form\Type\SearchFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: application_sonata_user_search }
application_sonata_user.form.handler.profile:
class: Application\Sonata\UserBundle\Form\Handler\ProfileFormHandler
arguments: ["@fos_user.profile.form", "@request", "@fos_user.user_manager", "@ewz_search.lucene"]
scope: request
public: false
答案 0 :(得分:1)
在services.yml文件中,我不得不放:
arguments: ["@sonata.user.profile.form", "@request", "@fos_user.user_manager", "@ewz_search.lucene"]
而不是
arguments: ["@fos_user.profile.form", "@request", "@fos_user.user_manager", "@ewz_search.lucene"]