3 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個贊
實(shí)際上這是因?yàn)?symfony 檢測到用戶實(shí)體中是否沒有“plainPassword”屬性。使用此“plainPassword”屬性的目的是作為臨時數(shù)據(jù),因此我們可以對其進(jìn)行編碼。您需要做的是在您的表單類型中設(shè)置映射為 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(),
]);
}

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個贊
問題在別處。
如果您調(diào)用不存在的“plainPassword”方法,請檢查您的樹枝或表單,并將其替換為“密碼”。
添加回答
舉報