我正在尝试更改用户的密码。在下拉列表中选择用户。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('password', PasswordType::class, array('label' => 'Password'))
->add('reset', SubmitType::class, array('label' => 'Reset'))
->add('username', EntityType::class, array('class' => User::class,
'query_builder' => function (EntityRepository $er) {return $er->createQueryBuilder('u')->orderBy('u.username', 'ASC');
},'choice_label'=>'username', 'label'=>'User'));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array('data_class' => User::class));
}
首次呈现表单时,我会看到用户列表。然后我填写新密码并提交。然后我得到错误:“在属性路径用户名处给出的类型为“字符串”、“对象”的预期参数”。错误发生在控制器中
$form->handleRequest($request);
这是控制器:
$form = $this->createForm(ResetPwdType::class, new User());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$u = $form->get('username')->getData();
$user = $this->getDoctrine()->getManager()->getRepository(User::class)->findOneBy(['username' => $u->getUsername()]);
$newPwd = $passwordEncoder->encodePassword($user, $form->get('password')->getData());
$user->setPassword($newPwd);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash('success', 'Password reset!');
$params = null;
return $this->redirectToRoute('reset_pwd');
}
return $this->render(
'security/resetpwd.html.twig',array('form' => $form->createView()));
我认为有一个属性需要是字符串而不是对象。这只是与用户名属性有关。我试图在用户实体上添加一个 toString 方法但没有成功。我必须改变什么才能摆脱错误并将新密码保存到数据库中。