在 user_name 的占位符中获取错误值
Getting wrong value in placeholder for user_name
我已经在 Symfony 中创建了一个用户配置文件页面。当我点击个人资料时,我需要看到 user_name、email 和 user_image url。除 user_name 外,一切正常。不再显示 name 而是再次显示 email... 就像您在图片中看到的那样。当我尝试更改某些内容并保存更改时,所有内容都会按预期更改和保存,但同样只有电子邮件可见。
Profile screenshot
这是我的控制器中用于配置文件路由的代码:
//==
//=== 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));
}
这是我在实际 Profile twig 文件中的代码:
{% 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 %}
在您的用户实体中,您可能有这样一个块:
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
您可以尝试创建这样的函数
public function getRealUserName(): string
{
return $this->userName;
}
然后调整您的表单,或者修改(但关心身份验证)您的 getUsername() 函数
我已经在 Symfony 中创建了一个用户配置文件页面。当我点击个人资料时,我需要看到 user_name、email 和 user_image url。除 user_name 外,一切正常。不再显示 name 而是再次显示 email... 就像您在图片中看到的那样。当我尝试更改某些内容并保存更改时,所有内容都会按预期更改和保存,但同样只有电子邮件可见。 Profile screenshot
这是我的控制器中用于配置文件路由的代码:
//==
//=== 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));
}
这是我在实际 Profile twig 文件中的代码:
{% 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 %}
在您的用户实体中,您可能有这样一个块:
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
您可以尝试创建这样的函数
public function getRealUserName(): string
{
return $this->userName;
}
然后调整您的表单,或者修改(但关心身份验证)您的 getUsername() 函数