Home > Software engineering >  Getting wrong value in placeholder for user_name
Getting wrong value in placeholder for user_name

Time:09-17

I have created a user profile page in Symfony. When I click on profile, I need to see user_name, email and user_image url. Everything works except the user_name. Instead of showing the name is shows the email again... Like you see in the picture. When I try to change something and save changed everything is changed and saved as supposed but again only email visible. Profile screenshot

This is the code I have in my Controller for the profile route:

//==
//=== My Profile ====
//==

#[Route('/profile/{id}', name: 'profile')]
public function profile($id, Request $request): Response
{
    $user = $this->getDoctrine()->getRepository(User::class)->find($id);
    $form = $this->createFormBuilder($user)
        ->add("user_name", TextType::class, array('attr' => array("class" => "form-control fw-light border-1 border-muted rounded-pill bg-light shadow-sm mt-3 text-muted", "style" => "margin-bottom:15px")))
        ->add("email", TextType::class, array('attr' => array("class" => "form-control fw-light border-1 border-muted rounded-pill bg-light shadow-sm mt-3 text-muted", "style" => "margin-bottom:15px")))
        ->add("user_image", TextType::class, array('attr' => array("class" => "form-control fw-light border-1 border-muted rounded-pill bg-light shadow-sm mt-3 text-muted", "style" => "margin-bottom:15px")))
        ->add("save", SubmitType::class, array('attr' => array("class" => "btn-outline-primary fw-light btn-sm border-1 shadow-sm rounded-pill m-3", "style" => "margin-bottom:15px"), "label" => "Save changes"))->getForm();
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $user_name = $form["user_name"]->getData();
        $email = $form["email"]->getData();
        $user_image = $form["user_image"]->getData();
       
        $user->setUserName($user_name);
        $user->setEmail($email);
        $user->setUserImage($user_image);

        $em = $this->getDoctrine()->getManager();

        $em->persist($user);
        $em->flush();

        $this->addFlash('notice', 'Profile Edited');

        return $this->redirectToRoute('meal');
    }
    return $this->render('meal/profile.html.twig', 
                        ["form" => $form->createView()]
    );
}


//=== Showing all the users===

#[Route('/manageusers', name: 'manageusers')]
public function manageusers(): Response
{
    $user = $this->getDoctrine()->getRepository('App:User')->findAll();
    return $this->render('meal/manageusers.html.twig', array('user' => $user));
}

And this is the code I have in the actual Profile twig file:

{% extends 'base.html.twig' %}

{% block title %}My Profile
{% endblock %}

{% block body %}

    <h1 class="page-header fw-light text-center py-5">
        Edit Profile
    </h1>
    <div class="container ">
        <div class="row d-flex justify-content-center">
            <div class="col-12 col-sm-10 col-md-8 col-lg-6 ">
                <div class="form mb-4">
                    {{ form_start(form) }}
                    {{ form_widget(form) }}
                    {{ form_end(form) }}
                </div>
            </div>
        </div>
    </div>

{% endblock %}

CodePudding user response:

In your User entity you might have a block like this :

/**
 * A visual identifier that represents this user.
 *
 * @see UserInterface
 */
public function getUsername(): string
{
    return (string) $this->email;
}

You could try to create a function like this

public function getRealUserName(): string
{
    return $this->userName;
}

And then adapt your form, or either modify (but care about authentification) your getUsername() function

  • Related