我的 Symfony RegistrationFormType 没有提交

Posted

技术标签:

【中文标题】我的 Symfony RegistrationFormType 没有提交【英文标题】:My Symfony RegistrationFormType does not submit 【发布时间】:2021-11-25 17:17:32 【问题描述】:

我使用 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('isbbethesda@gmail.com', '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 命令生成。请问问题出在哪里?

【问题讨论】:

我想您还需要在表单中注册一个SubmitType::class - documentation 【参考方案1】:

在您的表单中添加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) 

【讨论】:

我做到了,它不起作用。它似乎是我的代码中的一个普遍问题。因为,我在另一个表单类型中看到了同样的问题【参考方案2】:

您需要在表单类型中定义data_class

class RegistrationFormType extends AbstractType

    // ...

    public function configureOptions(OptionsResolver $resolver): void
    
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    

【讨论】:

我做到了,它不起作用。它似乎是我的代码中的一个普遍问题。因为,我在另一个表单类型中看到了同样的问题【参考方案3】:

我能够通过添加以下行来检测我的表单的提交

  $form->submit($request->request->get($form->getName()));

   

按照页面https://symfony.com/doc/current/form/direct_submit.html上的说明提交之前 感谢您的指导。希望这可以帮助任何面临这个问题的人。

【讨论】:

以上是关于我的 Symfony RegistrationFormType 没有提交的主要内容,如果未能解决你的问题,请参考以下文章

Symfony 3.4 在我的包中使用视图

为啥 Symfony 2 在我的环境中响应非常缓慢?

Symfony 5.3 新身份验证不保留我的身份验证

Symfony 2 还是 Symfony 1.4? [关闭]

Symfony 缓存:清除错误毁了我的整个项目

Symfony 3.4 功能测试