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

时间:2016-02-05 16:54:31

标签: php forms symfony

我正在关注A symfony tutorial in the official documentation。 但是,当我到达Field Type Options时,symfony会抛出错误 “在第13行的form_div_layout.html.twig中呈现模板(”注意:数组到字符串转换“)期间抛出了异常。”

我已经检查了google和拼写错误,这些违规行,来自文档似乎是:

->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 个答案:

没有答案