【问题标题】:PHP Symfony createFormBuilder ::add must be of the type string or null, array givenPHP Symfony createFormBuilder ::add 必须是字符串类型或 null,给定数组
【发布时间】:2021-01-21 14:17:50
【问题描述】:

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

传递给 Symfony\Component\Form\FormBuilder::add() 的参数 2 必须是字符串或 null 类型,给定数组,在 C:\xampp\htdocs\symphart\src\ 中调用Controller\ArticleController.php

怎么了?有人知道吗?

ArticleController 代码:

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

?>

【问题讨论】:

    标签: php forms symfony controller formbuilder


    【解决方案1】:

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

    【讨论】:

    • 好眼光!拼写错误通常不被认为是 stackoverflow 上的有效答案,但也许发帖者会回来接受它。
    • @Cerad 是的,但我没有注意到那个错字。 :)
    【解决方案2】:

    Symfonyv4.4 升级到v5.4 后,我遇到了类似的错误。

    罪魁祸首是:form getBlockPrefix() 方法(位于App/From/Type 文件夹中的ExampleType.php 文件中)。

    之前返回 null 有效。然而新版本需要返回string

    /**
     * @return string
     */
    public function getBlockPrefix(): string
    {
        return '';
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 1970-01-01
      • 2021-05-26
      • 2017-09-20
      • 2022-10-02
      • 2021-10-02
      • 2022-08-21
      • 1970-01-01
      • 2018-07-24
      相关资源
      最近更新 更多