Hello #Be;"," bundle \ FrontBundle \ Form \ Type \ PrestationType"特定

时间:2016-01-07 15:22:36

标签: php symfony doctrine-orm

我测试了我的应用程序,我发现了这个错误 请帮忙

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

这是我的输出错误

  

类型&#34;字符串&#34;的预期参数,   &#34;捆绑\ FrontBundle \表格\型号\ PrestationType&#34;给定

和堆栈跟踪

  

[1] Symfony \ Component \ Form \ Exception \ UnexpectedTypeException:Expected   类型&#34;字符串&#34;的参数,   &#34;捆绑\ FrontBundle \表格\型号\ PrestationType&#34;特定       在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),
     

&#39; 1&#39;)           在/Users/sylva/garage/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php   第62行

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

真)           在/Users/sylva/garage/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php   第169行

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

thanx guy

3 个答案:

答案 0 :(得分:4)

$ this-&gt; 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 :(得分:1)

考虑到您需要Image Form并使用Symfony版本&lt; = 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';
    }
}

答案 2 :(得分:0)

在处理symfony表单时,由于我使用的是HttpFoundationExtension,因此我还必须向其中添加相关的工厂,因此从

开始更改
$formFactory = Forms::createFormFactory();

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

如此处所写 https://symfony.com/doc/current/components/form.html

与HttpFoundation组件集成

如果使用HttpFoundation组件,则应将HttpFoundationExtension添加到表单工厂:

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

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

现在,当您处理表单时,可以将Request对象传递给handleRequest():

$form->handleRequest($request);