Здравствуйте, ожидаемый аргумент типа «строка», «bundle \ FrontBundle \ Form \ Type \ PrestationType» дан

1

Я тестирую свое приложение, и я получаю эту ошибку.

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

моя сущность

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


}

мой контроллер

<?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');
    }
}

и это моя ошибка вывода

Ожидаемый аргумент типа "string", "bundle\FrontBundle\Form\Type\PrestationType"

и трассировка стека

[1] Symfony\Component\Form\Exception\UnexpectedTypeException: ожидаемый аргумент типа "string", "bundle\FrontBundle\Form\Type\PrestationType", указанный в n/a в /Users/sylva/garage/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php строка 64

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

null, array()) в строке /Users/sylva/garage/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php 39

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

null, array()) в /Users/sylva/garage/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php строка 285

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

строка 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

строка 139

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

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

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

true) в /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 парень

  • 0
    Это форма для Prestation или Image?
Теги:
doctrine2

2 ответа

4

$this-> createForm() ожидает, что первым аргументом будет имя класса формы, которую вы хотите создать.

Заменить:

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

с

// for PHP 5.5+
$form = $this->createForm(PrestationType::class);
// for older versions
$form = $this->createForm('bundle\FrontBundle\Form\Type\PrestationType');
1

Учитывая, что вы хотите использовать форму изображения и используете версию Symfony <= 2.7. *:

В вашем контроллере передайте новый объект Image качестве второго аргумента метода createForm:

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

    /.../
}

В нашем 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';
    }
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню