数据库
首页 > 数据库> > php – 创建没有数据库的Symfony 4表单

php – 创建没有数据库的Symfony 4表单

作者:互联网

我是symfony的新手,想要创建一个简单的联系表单,获取提交的值并将其作为电子邮件发送.我不想在数据库中保存任何内容.

我按照本页面上的文档:https://symfony.com/doc/current/form/without_class.html

我使用上面的代码创建了一个新页面/操作:

/**
 * @Route("/contact", name="page_contact")
 */
public function contact(Request $request)
{
    $defaultData = ['message' => 'Type your message here'];
    $form = $this->createFormBuilder($defaultData)
        ->add('name', TextType::class)
        ->add('email', EmailType::class)
        ->add('message', TextareaType::class)
        ->add('send', SubmitType::class)
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // data is an array with "name", "email", and "message" keys
        $data = $form->getData();
    }

    return $this->render('Shop/contact.html.twig', [
        'form' => $form
    ]);
}

但这给了我一个错误:

Symfony\Component\Form\Exception\InvalidArgumentException:
Could not load type "Doctrine\DBAL\Types\TextType": class does not implement "Symfony\Component\Form\FormTypeInterface".

  at vendor/symfony/form/FormRegistry.php:89
  at Symfony\Component\Form\FormRegistry->getType('Doctrine\\DBAL\\Types\\TextType')
     (vendor/symfony/form/FormFactory.php:74)
  at Symfony\Component\Form\FormFactory->createNamedBuilder('name', 'Doctrine\\DBAL\\Types\\TextType', null, array())
     (vendor/symfony/form/FormBuilder.php:97)
  at Symfony\Component\Form\FormBuilder->create('name', 'Doctrine\\DBAL\\Types\\TextType', array())
     (vendor/symfony/form/FormBuilder.php:256)
  at Symfony\Component\Form\FormBuilder->resolveChildren()
     (vendor/symfony/form/FormBuilder.php:206)
  at Symfony\Component\Form\FormBuilder->getForm()
     (src/Controller/ShopController.php:68)
  at App\Controller\ShopController->contact(object(Request))
     (vendor/symfony/http-kernel/HttpKernel.php:149)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:66)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:190)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:37)

我究竟做错了什么?为什么堆栈跟踪中存在所有Doctrine内容?

解决方法:

您可能导入了错误的类型.代替:

use Doctrine\DBAL\Types\TextType;

你需要:

use Symfony\Component\Form\Extension\Core\Type\TextType;

标签:php,symfony,symfony-forms,symfony4
来源: https://codeday.me/bug/20190716/1474119.html