dseigqk7443 2016-02-05 16:54
浏览 66

PHP注意:在制作symfony表单模板时,“数组到字符串转换”

I'm following A symfony tutorial in the official documentation. However When I get to Field Type Options symfony throws the error "An exception has been thrown during the rendering of a template ("Notice: Array to string conversion") in form_div_layout.html.twig at line 13."

I have checked on google and typos, the offending line, from the documentation seems to be:

->add('dueDate', DateType::class, array('widget' => 'single_text'))

Which is straight from the documentation. For context the rest of the file looks like this:

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Task;

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
        ]);
    }

    /**
     * @Route("/form", name="formmma")
     */
    public function newAction(Request $request)
    {
        // create a task and give it some dummy data for this example
        $task = new Task();
        $task->setTask('Write a blog post');

        $task->setDueDate(new \DateTime('tomorrow'));

        $form = $this->createFormBuilder($task)
            ->add('task', TextType::class)
           // ->add('dueDate', DateType::class)
            ->add('dueDate', DateType::class, array('widget' => 'single_text'))
           // ->add('dueDate', null, array(
           //     'widget' => 'single_text',
           //     'required' => false
           // ))

            ->add('save', SubmitType::class, array('label' => 'Create Task'))
            ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
             // ... perform some action, such as saving the task to the database

             return $this->redirectToRoute('task_success');
        }

        return $this->render('default/new.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}
  • 写回答

1条回答 默认 最新

  • dongxiongshi9952 2016-08-13 20:40
    关注

    in your controller put

         if ($form->isSubmitted()) {
            $dateFrom=$booking->dateOfArrival;
            $booking->dateOfArrival= new \DateTime();
            $booking->dateOfArrival->setDate($dateFrom->format('Y'),$dateFrom->format('m'),$dateFrom->format('d'));}
    

    PS: "booking" is the model used in the form

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错