我使用 symfony 控制臺 make:registration 命令生成了用戶注冊碼。但是當我填寫注冊表并提交時,var_dump($ form->isSubmitted()) 總是顯示false,所以我無法實際創建用戶。這有什么可以解釋的?如何糾正?
這是生成的控制器檔案:
#[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
dd(1);
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
// generate a signed url and email it to the user
$this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
(new TemplatedEmail())
->from(new Address('[email protected]', 'Bethesda Administration'))
->to($user->getEmail())
->subject('Please Confirm your Email')
->htmlTemplate('registration/confirmation_email.html.twig')
);
// do anything else you need here, like send an email
return $this->redirectToRoute('app_home');
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
這是生成的表單型別:
class RegistrationFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email')
->add('agreeTerms', CheckboxType::class, [
'mapped' => false,
'constraints' => [
new IsTrue([
'message' => 'You should agree to our terms.',
]),
],
])
->add('plainPassword', PasswordType::class, [
// instead of being set onto the object directly,
// this is read and encoded in the controller
'mapped' => false,
'attr' => ['autocomplete' => 'new-password'],
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
])
;
}
}
這是生成的樹枝檔案:
{{ form_start(registrationForm) }}
{{ form_row(registrationForm.email) }}
{{ form_row(registrationForm.plainPassword, {
label: 'Password'
}) }}
{{ form_row(registrationForm.agreeTerms) }}
<button type="submit" class="btn">Register</button>
{{ form_end(registrationForm) }}
這個檔案是由symfony console make:registration命令生成的。請問問題出在哪里?
uj5u.com熱心網友回復:
在您的表單中添加一個SubmitType:
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
//...
$builder
->add('email')
->add('agreeTerms', CheckboxType::class, [
'mapped' => false,
'constraints' => [
new IsTrue([
'message' => 'You should agree to our terms.',
]),
],
])
->add('plainPassword', PasswordType::class, [
// instead of being set onto the object directly,
// this is read and encoded in the controller
'mapped' => false,
'attr' => ['autocomplete' => 'new-password'],
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
])
->add('submit', SubmitType::class, [
'attr' => ['class' => 'btn'],
])
;
并更新您的樹枝檔案:
{{ form_start(registrationForm) }}
{{ form_row(registrationForm.email) }}
{{ form_row(registrationForm.plainPassword, {
label: 'Password'
}) }}
{{ form_row(registrationForm.agreeTerms) }}
{{ form_row(registrationForm.submit) }}
{{ form_end(registrationForm) }}
uj5u.com熱心網友回復:
您需要data_class在表單型別中定義。
class RegistrationFormType extends AbstractType
{
// ...
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
uj5u.com熱心網友回復:
通過添加以下幾行,我能夠檢測到表單的提交
$form->submit($request->request->get($form->getName()));
在按照頁面https://symfony.com/doc/current/form/direct_submit.html上的指定提交之前,
感謝您的指導。愿這對任何將面臨此問題的人有所幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/322542.html
標籤:php symfony 枝条 登记 symfony 形式
