dougai2427 2016-09-12 13:54
浏览 57
已采纳

FormFactory.php中的UnexpectedTypeException

I'am under Silex ~2.0. I have a problem with FormServiceProvider, I got this error :

UnexpectedTypeException in FormFactory.php line 64: Expected argument of type "string", "SocialWall\Form\Type\CommentType" given

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

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

Form/Type/CommentType.php

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

I have another error : With PhpStorm I see handleRequest, isSubmitted, isValid and createView methods are not found.

Please save my day !

</div>
  • 写回答

2条回答 默认 最新

  • duan0424 2016-09-12 19:10
    关注

    Ok, I fixe it, on route.php line 23 :

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

    On CommentType.PHP :

    <?php
    
    namespace SocialWall\Form\Type;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\Extension\Core\Type\TextareaType;
    
    
    class CommentType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('content', TextareaType::class);
        }
    
        public function getName()
        {
            return 'comment';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看