简体   繁体   中英

Overriding SonataUserBundle's ProfileFormType

I'm trying to override SonataUserBundle's ProfileFormType. I have added some extra fields to that form and all the fields render on the page. So that works. But now I'm wondering why the user's data doesn't load, as the firstname, lastname, ... is already known but just not shown in the textboxes of the form.

The overridden ProfileController class's 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')
    ));
} 

The overridden ProfileFormHandler class's process function:

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

In the services.yml file, I had to put:

arguments: ["@sonata.user.profile.form", "@request", "@fos_user.user_manager", "@ewz_search.lucene"] 

instead of

arguments: ["@fos_user.profile.form", "@request", "@fos_user.user_manager", "@ewz_search.lucene"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM