3 回答

TA贡献1824条经验 获得超5个赞
实际上这是因为 symfony 检测到用户实体中是否没有“plainPassword”属性。使用此“plainPassword”属性的目的是作为临时数据,因此我们可以对其进行编码。您需要做的是在您的表单类型中设置映射为 false的“plainPassword”属性。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'mapped' => false,
'first_options' => array('label' => 'New password'),
'second_options' => array('label' => 'Confirm new password'),
'invalid_message' => 'The password fields must match.',
))
;
}
并在您的控制器中将“plainPassword”编码为“密码”:
/**
* @Route("/register", name="app_register")
*/
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
return $this->redirectToRoute('any_route');
}
return $this->render('registration/register.html.twig', [
'form' => $form->createView(),
]);
}
添加回答
举报