Clément Andraud
Clément Andraud

Reputation: 9269

How to create a FormType without any entity?

I just created a new form in AppBundle/Form/Type named ContactType.php

It's just a form with a field email and message, to send an email.

How can I instantiate this form in my controller (I don't have any entites linked with) ? I tried somethings like :

$form = $this->createForm(new ContactType());

But symfony wants a string for argument of createForm()... Thanks !

Upvotes: 1

Views: 1466

Answers (1)

xabbuh
xabbuh

Reputation: 5881

Just use the fully qualified class name of your form type class like this (you may have to adapt the use statement to your needs):

use AppBundle\Form\Type\ContactType;

$form = $this->createForm(ContactType::class);

Using the class constant requires you to use PHP 5.5 or higher. If you still use PHP 5.3 or 5.4, you will need to use the FQCN as a string:

$form = $this->createForm('AppBundle\Form\Type\ContactType');

Upvotes: 3

Related Questions