Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3 form type error

Tags:

php

symfony

I have the entity "News" with fields:

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=100)
 */
protected $name;


/**
 * @ORM\Column(type="text")
 */
protected $description;

I generate the form in my controller:

public function createNewsAction(Request $request)
{
    $news = new News();
    $news->setName('New!');
    $news->setDescription('test');
    $form = $this->createFormBuilder($news)
        ->add('name', TextType::class)
        ->add('description', TextType::class)
        ->add('save', SubmitType::class, array('label' => 'Create Task'))
        ->getForm();
    return $this->render('frontend/createNews.html.twig', array(
        'form' => $form->createView(),
    ));
}

And when I try to call this method ("createNewsAction") I got the error:

Could not load type "Doctrine\DBAL\Types\TextType"

500 Internal Server Error - InvalidArgumentException

Stack Trace

in vendor\symfony\symfony\src\Symfony\Component\Form\FormRegistry.php at line 87   -
                if (class_exists($name) && in_array('Symfony\Component\Form\FormTypeInterface', class_implements($name))) {
                    $type = new $name();
                } else {
                    throw new InvalidArgumentException(sprintf('Could not load type "%s"', $name));
                }
            }

What I do wrong with this code?

like image 666
raskopin Avatar asked Mar 13 '16 14:03

raskopin


1 Answers

Check your use statement at the top of your controller...
You've probably used:

use Doctrine\DBAL\Types\TextType

It should be:

use Symfony\Component\Form\Extension\Core\Type\TextType;
like image 56
slawisha Avatar answered Oct 19 '22 04:10

slawisha