PHP Symfony createFormBuilder ::add 必须是 string 或 null 类型,给定数组

问题描述

伙计们! 我使用本地网络服务器 (localhost) 开发了一个网页。我使用 PHP、Symfony、Twig 和 Doctrine。 当我完成我可以去的页面时,如果我按下主页上的“新文章”按钮,我遇到了这个问题(新文章页面将在我的主页中创建一个文章,因此我尝试使用表单构建器创建一个表单,我认为 createFormbuilder 方法中的那个是错误的):

传递给 Symfony\Component\Form\FormBuilder::add() 的参数 2 必须是 string 或 null 类型,给定数组,在 C:\xampp\htdocs\symphart\src\ 中调用控制器\文章控制器.PHP

怎么了?有人知道吗?

文章控制器代码

   namespace App\Controller;

   use App\Entity\Article;

   use Symfony\Component\HttpFoundation\Response;
   use Symfony\Component\HttpFoundation\Request;
   use Symfony\Component\Routing\Annotation\Route;
   use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
   use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

   use Symfony\Components\Form\Extension\Core\Type\TextType;
   use Symfony\Components\Form\Extension\Core\Type\TextareaType;
   use Symfony\Components\Form\Extension\Core\Type\SubmitType;

   class ArticleController extends AbstractController {
       /**
        * @Route ("/",name = "article_list")
        * @Method ({"GET"})
        */
       public function index ()
       {
           $articles = $this->getDoctrine()->getRepository(Article ::class)->findAll();
           return $this->render('Articles/index.html.twig',array ('articles' => $articles));
       }

       /**
        * @Route ("/article/new",name = "new_article")
        * @Method ({"GET","POST"})
        */
       public function new (Request $request)
       {
           $article = new Article ();
           $form = $this->createFormBuilder($article)
               ->add ('title',TextType::class,array ('attr' => array ('class' => 'form-control')))
               ->add('body'. TextareaType::class,array ('required' => false,'attr' => array ('class' => 'form-control' )))
               ->add ('save',SubmitType::class,array ('label' => 'Create ','attr' => array ('class' => 'btn btn-primary mt-3')))
               ->getForm();

               return $this->render ('articles/new.html.twig',array ('form' => $form->createView()));
       }


       /**
        * @Route ("/article/{id}",name = "article_show")
        */
       public function show ($id)
       {
           $article = $this->getDoctrine()->getRepository(Article::class)->find($id);
           return $this->render ('articles/show.html.twig',array ('article' => $article));
       }

       

       ///**
        //* @Route ("/article/save")
        //*/
       //public function save ()
       //{
       //    $entityManager = $this->getDoctrine()->getManager();
       //    $article = new Article();
       //    $article->setTitle("Article Three");
       //    $article->setBody("This is body for Article Three");

       //    $entityManager->persist($article);

       //    $entityManager->flush();

       //    return new Response ('Saves an article with the id of '. $article->getId());
       //}
   }

?>

解决方法

->add('body' 后面有一个点;)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...