Php – Expected argument of type “string”, “Vendor\NameBundle\Form\EntitynameType” given Symfony 3.0

formbuilderPHPsymfony

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;
    }


}

Best Answer

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

Related Topic