FormFactory.php 中的 UnexpectedTypeException
UnexpectedTypeException in FormFactory.php
我正在使用 Silex ~2.0。
我的 FormServiceProvider 有问题,我收到了这个错误:
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
我的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';
}
}
我还有一个错误:
使用 PhpStorm,我发现 handleRequest、isSubmitted、isValid 和 createView 方法未找到。
请拯救我的一天!
createBuilder
方法的第一个参数应该是表示表单类型的字符串。
->createBuilder('form', object(Comment), array())
http://api.symfony.com/3.1/Symfony/Component/Form/FormFactory.html#method_createBuilder
好的,我在 route.php 第 23 行修复它:
$commentForm = $app['form.factory']->create(CommentType::class, $comment);
在 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';
}
}
我正在使用 Silex ~2.0。 我的 FormServiceProvider 有问题,我收到了这个错误:
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
我的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';
}
}
我还有一个错误: 使用 PhpStorm,我发现 handleRequest、isSubmitted、isValid 和 createView 方法未找到。
请拯救我的一天!
createBuilder
方法的第一个参数应该是表示表单类型的字符串。
->createBuilder('form', object(Comment), array())
http://api.symfony.com/3.1/Symfony/Component/Form/FormFactory.html#method_createBuilder
好的,我在 route.php 第 23 行修复它:
$commentForm = $app['form.factory']->create(CommentType::class, $comment);
在 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';
}
}