3

I'm new to symfony and want to to create a simple contact form, take the submitted values and send them as an Email. I don't want to save anything in the database.

I followed the documentation on this page: https://symfony.com/doc/current/form/without_class.html

I created a new page/action with the code from the page above:

    /**
     * @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
        ]);
    }

But this gives me an error:

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)

What am I doing wrong? Why is there all the Doctrine stuff in the Stack Trace?

2
  • Your IDE should have warned you about the namespace issues. Commented Jan 31, 2018 at 12:15
  • Im using phpStorm with symfony plugin. But the wrong namespace is valid and ->add('name', TextType::class) just takes the the wrong class as parameter, I don't use any methods of TextType. Commented Jan 31, 2018 at 14:23

2 Answers 2

15

You probably imported the wrong type. Instead of:

use Doctrine\DBAL\Types\TextType;

you need:

use Symfony\Component\Form\Extension\Core\Type\TextType;
Sign up to request clarification or add additional context in comments.

Comments

1

'form' => $form->createView() instead of 'form' => $form

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.