Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3 - Could not load type form type

Tags:

php

symfony

I have just updated symfony from 2.7 to 3.0 and got some troubles with it..

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

services.xml

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

Thats how im trying to create 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';
    }
}

Getting next error:

An exception has been thrown during the rendering of a template ("Could not load type "app_search"") in ....

How it should look like in symfony 3.0?

Thanks !

like image 441
Tigran Avatar asked Feb 22 '16 18:02

Tigran


1 Answers

You should change your settings to..

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

And in your form type

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());

The long and short of it is that forms aren't named any more, they are referenced by the class name.

like image 139
qooplmao Avatar answered Oct 03 '22 07:10

qooplmao