Nassim Abid
Nassim Abid

Reputation: 21

How to use a custom FormType in Symfony 3

I m training but I'm under symfony 3

i have problem i get this error

Expected argument of type "string", "Test\FrontBundle\Form\Type\SheetType" given

the code on SheetType.php is

<?php

namespace Test\FrontBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\FormBuilderInterface;


class SheetType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name',null,array('label'=>'Titre de l\'album'))
            ->add('type')
            ->add('artist')
            ->add('duration')
            ->add('released', DateType::class)
            ;
    }
}

and on my SheetController.php i do that form my controller i dont know how i can solve this all time i try else i got error

 public function createAction(Request $request)
    {
        $form = $this->createForm(new SheetType());

        $form->handleRequest($request);

        if($request->isMethod('post') && $form->isValid()){
            $em = $this->getDoctrine()->getManager();
            $em->persist($form->getData());
            $em->flush();
           return $this->redirect($this->generateUrl('test_front_sheet_list'));
        } 
        return $this->render('TestFrontBundle:Sheet:create.html.twig', array('form' => $form->createView()));
    }

Upvotes: 2

Views: 548

Answers (1)

Heah
Heah

Reputation: 2389

Since symfony 2.8 you have to pass a full qualified class name instance as argument when create a form or form builder, it does not take an instance of FormTypeInterface anymore.

see https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md

So you should use $form = $this->createForm(SheetType::class); instead.

Upvotes: 6

Related Questions