A
A
Alex Kay2016-05-14 00:00:57
symfony
Alex Kay, 2016-05-14 00:00:57

When editing a username, if the new name is taken, an error message is displayed, and... and does the current user's name change in the site header?

In symfony3 I make a user profile, authorization occurs by email, username is just a unique username.

public function profileAction(Request $request)
{
        $em = $this->getDoctrine()->getManager();
        $user = $em->getRepository('UserBundle:User')->findOneByToken($this->getUser()->getToken());

        $form = $this->createForm(UserProfileType::class, $user);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em->flush();
            $this->addFlash('success', self::MSG_SUCCESS_EDIT);
        }

        return [
            'form' => $form->createView()
        ];
}

Everything works fine if the username entered is unique.
But if the entered username has already been used:
  1. An error message is displayed - as expected.
  2. In the header of the site, where the name of the current authorized user (app.user) should be displayed in the template, the newly entered non-unique name is displayed - this is wrong, how, why?

And after that, with any action, a redirect to authorization will occur.
I tried to send a clone of the user to the form
$form = $this->createForm(UserProfileType::class, clone $user);

But in this case, the uniqueness check is triggered and a message appears that the name of the current user is already taken.
I think that you can get around the problem by using ajax check for uniqueness before submitting the form. But I think that the server part should work correctly in any case.
I ask for qualified help!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question