2016-02-05 97 views
1

我正在關注A symfony tutorial in the official documentation。 但是,當我到Field Type Options symfony引發錯誤 「在第13行的form_div_layout.html.twig中呈現模板(」注意:數組到字符串轉換「)期間拋出異常。」PHP注意:在創建symfony表單模板時'Array to string conversion'

我檢查了谷歌和錯別字,違規行,從文檔似乎是:

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

這是直接從文檔。 對於上下文中的文件的其餘部分是這樣的:

<?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(), 
     )); 
    } 
} 
+0

哪裏是你的陣列中使用的模型? ?? – devpro

+0

拋出錯誤的數組是「array('widget'=>'single_text')」 – user3784864

+0

我在我的測試環境中嘗試了完全相同的代碼,它工作。所以恕我直言,你應該做兩件事 - 1)檢查是否form_div_layout.html.twig沒有修改,2)清除symfony緩存 –

回答

0

在控制器把

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

PS:「預約」的形式是