传递给 Symfony\Component\Form\FormRenderer::renderBlock() 的参数 1 必须是 ...\FormView 的实例,给定的 ...\Form 的实例

2022-08-30 19:13:42

整个错误是 missiong 命名空间 Symfony\Component\Form,由于标题最大字符数,它被替换为 3 个点。

因此,我正在按照文档中提供的步骤进行操作,并且我无法找到我所遇到的错误的来源。如果有人能帮忙,我将不胜感激。

这是来自我的AuthController的方法

/**
 * @Route("/register", name="registrationPage")
 */
public function showRegistrationPage(Request $request)
{
    return $this->render('auth/register.html.twig', [
        'register_form' => $this->createForm(RegisterType::class, (new UserInformation()))
    ]);
}

这是方法,我声明表单

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('firstname', TextType::class, ['attr' => ['class' => 'form-control']])
        ->add('secondname', TextType::class, ['attr' => ['class' => 'form-control']])
        ->add('email', EmailType::class, ['attr' => ['class' => 'form-control']])
        ->add('password', PasswordType::class, ['attr' => ['class' => 'form-control']])
        ->add('password_confirmation', PasswordType::class, [
            'label' => 'Confirm Password',
            'attr' => ['class' => 'form-control'],
            'mapped' =>false
        ])
        ->add('Register', SubmitType::class, ['attr' => ['class' => 'btn btn-primary']]);

}

答案 1
/**
 * @Route("/register", name="registrationPage")
 */
public function showRegistrationPage(Request $request)
{
    $form = $this->createForm(RegisterType::class, (new UserInformation()));

    return $this->render('auth/register.html.twig', [
        'register_form' => $form->createView()
    ]);
}

http://symfony.com/doc/current/forms.html#building-the-form


答案 2

缺少的部分是 createView() 方法

/**
 * @Route("/register", name="registrationPage")
 */
public function showRegistrationPage(Request $request)
{
    return $this->render('auth/register.html.twig', [
        'register_form' => $this->createForm(RegisterType::class, (new UserInformation()))->createView()
    ]);
}

推荐