Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected argument of type "string", "Vendor\NameBundle\Form\EntitynameType" given Symfony 3.0

Because of the problems I had with symfony version 2.7 (404 page error right away after installing a project) I started using Symfony version 3.0. After some minor problems I figured out that "app/console" is replaced by "bin/console". So I'm working now on a new project and I have already build a new bundle with 1 entity called

Codeit/RestaurantBundle && CodeitRestaurantBundle:Reserveren

Format is annotation, and the entity has an id and 1 field called "naam" (string, 255). I updated the schema's, I generate the entities of Codeit and after that was succesfully done I generated a crud with write actions. The format was again annotation and the prefix is /reserveren.

So if I visit the page web/reserveren I am getting a show page of my entity. Unfortunately if I try to add a new entry I am getting the following error:

Expected argument of type "string", "Codeit\RestaurantBundle\Form\ReserverenType" given

My Bundle/Form/ReserverenType.php

<?php

namespace Codeit\RestaurantBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ReserverenType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('naam')
    ;
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Codeit\RestaurantBundle\Entity\Reserveren'
    ));
}
}

My entity code

<?php

namespace Codeit\RestaurantBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Reserveren
 *
 * @ORM\Table(name="reserveren")
 * @ORM\Entity(repositoryClass="Codeit\RestaurantBundle\Repository\ReserverenRepository")
 */
class Reserveren
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="Naam", type="string", length=255)
     */
    private $naam;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set naam
     *
     * @param string $naam
     *
     * @return Reserveren
     */
    public function setNaam($naam)
    {
        $this->naam = $naam;

        return $this;
    }

    /**
     * Get naam
     *
     * @return string
     */
    public function getNaam()
    {
        return $this->naam;
    }


}
like image 829
Giesburts Avatar asked Dec 09 '15 14:12

Giesburts


3 Answers

Forms have changed quite a bit in 3.0. You might be better off sticking with 2.8 for now.

You did not show it but I suspect, based on the error message, that your controller code looks like:

$form = $this->createForm(new ReservernType(), $item);

That is the 2.x way of doing things. For 3.x use:

$form = $this->createForm(ReservernType::class, $item);

http://symfony.com/doc/current/book/forms.html#creating-form-classes

like image 94
Cerad Avatar answered Oct 05 '22 23:10

Cerad


try with:

$builder
        ->add('naam', TextType::class);
        // If you use PHP 5.3 or 5.4 you must use
        // ->add('naam','Symfony\Component\Form\Extension\Core\Type\TextType')

instead of this

$builder
    ->add('naam');

And add the use statement:

use Symfony\Component\Form\Extension\Core\Type\TextType;

Motivation: from the upgrade guide:

Type names were deprecated and will be removed in Symfony 3.0. Instead of referencing types by name, you should reference them by their fully-qualified class name (FQCN) instead. With PHP 5.5 or later, you can use the "class" constant for that:

Hope this help

like image 33
Matteo Avatar answered Oct 05 '22 22:10

Matteo


Try like this:

/.../

use Symfony\Component\Form\Extension\Core\Type\TextType; 

/.../

class ReserverenType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
           $builder->add('naam', TextType::class);
    }
}
like image 43
scoolnico Avatar answered Oct 05 '22 23:10

scoolnico