FormFactory.php中的UnexpectedTypeException

时间:2016-09-12 13:54:53

标签: php symfony silex

我在Silex~2.0之下。 我有FormServiceProvider的问题,我收到了这个错误:

  

FormFactory.php第64行中的UnexpectedTypeException:类型"字符串"," SocialWall \ Form \ Type \ CommentType"的预期参数给定

in FormFactory.php line 64
at FormFactory->createBuilder(object(CommentType), object(Comment), array()) in FormFactory.php line 39
at FormFactory->create(object(CommentType), object(Comment)) in routes.php line 23
at {closure}('2', object(Request))
at call_user_func_array(object(Closure), array('2', object(Request))) in HttpKernel.php line 153
at HttpKernel->handleRaw(object(Request), '1') in HttpKernel.php line 68
at HttpKernel->handle(object(Request), '1', true) in Application.php line 496
at Application->handle(object(Request)) in Application.php line 477
at Application->run() in index.php line 11

我的route.php



<?php

use Symfony\Component\HttpFoundation\Request;
use SocialWall\Domain\Comment;
use SocialWall\Form\Type\CommentType;

// Home page
$app->get('/', function () use ($app) {
    $articles = $app['dao.article']->findAll();
    return $app['twig']->render('index.html.twig', array('articles' => $articles));
})->bind('home');

// Article details with comments
$app->match('/article/{id}', function ($id, Request $request) use ($app) {
    $article = $app['dao.article']->find($id);
    $commentFormView = null;
    if ($app['security.authorization_checker']->isGranted('IS_AUTHENTICATED_FULLY')) {
        // A user is fully authenticated : he can add comments
        $comment = new Comment();
        $comment->setArticle($article);
        $user = $app['user'];
        $comment->setAuthor($user);
        $commentForm = $app['form.factory']->create(new CommentType(), $comment);
        $commentForm->handleRequest($request);
        if ($commentForm->isSubmitted() && $commentForm->isValid()) {
            $app['dao.comment']->save($comment);
            $app['session']->getFlashBag()->add('success', 'Your comment was succesfully added.');
        }
        $commentFormView = $commentForm->createView();
    }
&#13;
&#13;
&#13;

窗体/类型/ CommentType.php

&#13;
&#13;
<?php

namespace SocialWall\Form\Type;

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


class CommentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('content', 'textarea');
    }

    public function getName()
    {
        return 'comment';
    }
}
&#13;
&#13;
&#13;

我有另一个错误: 使用PhpStorm我看到handleRequest,isSubmitted,isValid和createView方法都找不到。

请保存我的一天!

2 个答案:

答案 0 :(得分:0)

createBuilder方法的第一个参数应该是表示表单类型的字符串。

->createBuilder('form', object(Comment), array())

http://api.symfony.com/3.1/Symfony/Component/Form/FormFactory.html#method_createBuilder

答案 1 :(得分:0)

好的,我在route.php第23行修理它:

echo=

在CommentType.PHP上:

$commentForm = $app['form.factory']->create(CommentType::class, $comment);