UnexpectedTypeException в FormFactory.php

Я нахожусь под Silex ~2.0. У меня проблема с FormServiceProvider, я получил эту ошибку:

UnexpectedTypeException в строке FormFactory.php 64: указан ожидаемый аргумент типа "строка", "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();
    }

Форма / тип /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 не найдены.

Пожалуйста, сохраните мой день!

2 ответа

Решение

Хорошо, я исправляю это в строке 23 route.php:

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

Первый параметр createBuilder Метод должен быть строкой, представляющей тип формы.

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

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

Другие вопросы по тегам