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 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了