Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hello Expected argument of type "string", "bundle\FrontBundle\Form\Type\PrestationType" given

I test my application and i'm this error Help please

my FormType

namespace bundle\FrontBundle\Form\Type;



use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PrestationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
            $builder
                ->add('path')
                ->add('alt')
                ->add('save', 'submit');
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(

            'data_class' => 'bundle\FrontBundle\Entity\Image',

        ));

    }

    public function getName()
    {
        return 'prestation';
    }
}

my Entity

<?php

namespace bundle\FrontBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Image
 *
 * @ORM\Table(name="image")
 * @ORM\Entity(repositoryClass="bundle\FrontBundle\Repository\ImageRepository")
 */
class Image
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

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


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


}

my controller

<?php

namespace bundle\FrontBundle\Controller;


use bundle\FrontBundle\Entity\Image;
use bundle\FrontBundle\Entity\Prestation;
use bundle\FrontBundle\Form\Type\PrestationType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class FrontController extends Controller
{
    public function indexAction(Request $request)
    {


        $form = $this->createForm(new PrestationType());

        $form->handleRequest($request);

        return $this->render('FrontBundle::index.html.twig', array('form' => $form->createView()));
    }


    public function listAction()
    {

        return $this->render('FrontBundle:Pages:list.html.twig');
    }
}

and this my output error

Expected argument of type "string", "bundle\FrontBundle\Form\Type\PrestationType" given

and Stack Trace

[1] Symfony\Component\Form\Exception\UnexpectedTypeException: Expected argument of type "string", "bundle\FrontBundle\Form\Type\PrestationType" given at n/a in /Users/sylva/garage/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 64

at Symfony\Component\Form\FormFactory->createBuilder(object(PrestationType),

null, array()) in /Users/sylva/garage/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 39

at Symfony\Component\Form\FormFactory->create(object(PrestationType),

null, array()) in /Users/sylva/garage/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 285

at Symfony\Bundle\FrameworkBundle\Controller\Controller->createForm(object(PrestationType))
    in /Users/sylva/garage/src/bundle/FrontBundle/Controller/FrontController.php

line 19

at bundle\FrontBundle\Controller\FrontController->indexAction(object(Request))
    in  line 

at call_user_func_array(array(object(FrontController), 'indexAction'), array(object(Request)))
    in /Users/sylva/garage/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php

line 139

at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request),

'1') in /Users/sylva/garage/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 62

at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1',

true) in /Users/sylva/garage/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 169

at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
    in /Users/sylva/garage/web/app_dev.php line 30

thanx guy

like image 323
Darkness007 Avatar asked Dec 06 '25 03:12

Darkness007


1 Answers

$this->createForm() expects first argument to be the class name of the form you want to instantiate.

Replace:

$form = $this->createForm(new PrestationType());

with

// for PHP 5.5+
$form = $this->createForm(PrestationType::class);
// for older versions
$form = $this->createForm('bundle\FrontBundle\Form\Type\PrestationType');
like image 196
jedrzej.kurylo Avatar answered Dec 09 '25 20:12

jedrzej.kurylo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!