Symfony 3 - Failed to load form type type

I just upgraded symfony from 2.7 to 3.0 and got some problems with it.

It cannot load my form types. Here is an example.

services.xml

app.search: class: AppBundle\Form\Type\SearchFormType tags: - { name: form.type, alias: app_search } 

This is how I try to create a form.

 $form = $this->createForm('app_search', new Search()); 

SearchFormType

 namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; class SearchFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('phrase', 'text'); } public function getBlockPrefix() { return 'app_search'; } } 

The following error:

An exception was thrown during the rendering of the template ("Failed to load type" app_search "") ....

How should it look in Symfony 3.0?

Thanks!

+7
php symfony
source share
2 answers

You must change your settings to.

 app.search: class: AppBundle\Form\Type\SearchFormType tags: - { name: form.type } 

And in your form like

 use Symfony\Component\Form\Extension\Core\Type\TextType; // ... $builder->add('phrase', TextType::class); 

Then, to call it, use ...

 $form = $this->createForm(SearchFormType::class, new Search()); // or $form = $this->createForm('AppBundle\Form\Type\SearchFormType', new Search()); 

Long and short is that the forms are no longer called; they are referenced by the class name.

+7
source share

I am stuck with this for about 2 hours today. And the problem just came from the form type namespace. Instead of using namespace AppBundle\Form\Type; in your SearchFormType.php file write namespace AppBundle\Form; .

+5
source share

All Articles