2016-09-12 1 views
0

I'm under Silex ~ 2.0. У меня есть проблема с FormServiceProvider, я получил эту ошибку:UnexpectedTypeException в FormFactory.php

UnexpectedTypeException в FormFactory.php линии 64: Ожидаемый аргумент типа "строка", "SocialWall \ Form \ Тип \ комментарийВведите" данный

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 не найдены.

Сохраните мой день!

ответ

0

Хорошо, я фиксируем его на 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'; 
    } 
} 
0

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

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

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

+0

Большое спасибо за помощь! Но как я это делаю с моим кодом? Потому что я использую создание fonction. –

+0

'create' имеет те же параметры http://api.symfony.com/3.1/Symfony/Component/Form/FormFactory.html#method_create – malcolm

+0

Да, я вижу это, но как я могу адаптировать эту строку: $ commentForm = $ app ['form.factory'] -> create (new CommentType(), $ comment); ? –

Смежные вопросы