3
votes

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

3
Is it a form for Prestation or Image?scoolnico

3 Answers

4
votes

$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');
1
votes

Considering you want Image Form and use Symfony version <= 2.7.* :

In your controller, pass a new Image object as second argument of the createForm method:

namespace bundle\FrontBundle\Controller;

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

class FrontController extends Controller
{
    public function indexAction(Request $request)
    {
        $image = new Image();

        $form = $this->createForm(new ImageType(), $image);

        $form->handleRequest($request);

        if ($form->isValid()) {

           // Your code... e.g persist & flush Entity... 

           return $this->redirectToRoute('task_success');
        }

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

    /.../
}

In our ImageType:

namespace bundle\FrontBundle\Form\Type;

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

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

    public function configureOptions(OptionsResolver $resolver)
    {
         $resolver->setDefaults([
             'data_class' => 'bundle\FrontBundle\Entity\Image'
         ]);
    }

    public function getName()
    {
        return 'image';
    }
}
0
votes

When dealing with symfony forms and since I was using HttpFoundationExtension I had to add also the pertinent factory to it, so changing from

$formFactory = Forms::createFormFactory();

to

$formFactory = Forms::createFormFactoryBuilder()
    ->addExtension(new HttpFoundationExtension())
    ->getFormFactory();

as written in here https://symfony.com/doc/current/components/form.html

Integration with the HttpFoundation Component

If you use the HttpFoundation component, then you should add the HttpFoundationExtension to your form factory:

use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
use Symfony\Component\Form\Forms;

$formFactory = Forms::createFormFactoryBuilder()
    ->addExtension(new HttpFoundationExtension())
    ->getFormFactory();

Now, when you process a form, you can pass the Request object to handleRequest():

$form->handleRequest($request);