我正在尝试覆盖 SonataUserBundle 的 ProfileFormType。我在该表单中添加了一些额外的字段,并且所有字段都呈现在页面上。所以这行得通。但是现在我想知道为什么用户的数据没有加载,因为名字、姓氏……是已知的,但只是没有显示在表单的文本框中。
被覆盖的 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;
}
服务.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