复制演示时出错"在映射时无法定义序列项"

B R*_*Rad 5 php forms symfony

我在尝试重现Symfony提供的演示时遇到错误.你可以在这里找到它. http://symfony.com/doc/current/book/forms.html#book-form-creating-form-classes

当我在控制器中包含表单时,我可以让表单工作得很好但是当我将表单作为自己的类时,我最终会得到一个错误.

在映射500内部服务器错误 - ParseException时,您无法定义序列项

日志返回:

CRITICAL - 未捕获的PHP异常Symfony\Component\Yaml\Exception\ParseException:"在映射中无法定义序列项"在/vagrant/vendor/symfony/symfony/src/Symfony/Component/Yaml/Parser.php第81行

我似乎无法找到问题所在.

Task.php文件:

    <?php

namespace Acme\TaskBundle\Entity;

class Task
{
    protected $task;

    protected $dueDate;

    public function getTask()
    {
        return $this->task;
    }
    public function setTask($task)
    {
        $this->task = $task;
    }

    public function getDueDate()
    {
        return $this->dueDate;
    }
    public function setDueDate(\DateTime $dueDate = null)
    {
        $this->dueDate = $dueDate;
    }
}
Run Code Online (Sandbox Code Playgroud)

DefaultController.php:

<?php
namespace Acme\TaskBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\TaskBundle\Entity\Task;
use Symfony\Component\HttpFoundation\Request;
use Acme\TaskBundle\Form\Type\TaskType;

class DefaultController extends Controller
{
    public function newAction(Request $request)
    {
        // create a task and give it some dummy data for this example
        $task = new Task();

        $form = $this->createForm(new TaskType(), $task);

        $form->handleRequest($request);

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

            return $this->redirect($this->generateUrl('task_success'));
        }

        return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

TaskType.php:

<?php

// src/Acme/TaskBundle/Form/Type/TaskType.php
namespace Acme\TaskBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('task')
            ->add('dueDate', null, array('mapped' => false))
            ->add('save', 'submit');
    }

    public function getName()
    {
        return 'task';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TaskBundle\Entity\Task',
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

需要帮助请叫我.我在多个文件中尝试过此设置.它必须是小事.它就在Symfony的网站上.


编辑


我唯一的YML文件是我在他们的教程中使用的验证文件.validation.yml文件

# Acme/TaskBundle/Resources/config/validation.yml
Acme\TaskBundle\Entity\Task:
    properties:
        task:
            - NotBlank: ~
        dueDate:
            - NotBlank: ~
            - Type: \DateTime
Run Code Online (Sandbox Code Playgroud)

可能问题是我没有定义数组的yml文件?

qoo*_*mao 5

你的 yml 文件中有这样的东西吗?

stuff:
    thing1: one      // mapping
    thing2: two      // mapping
    thing3: three    // mapping
    - four           // sequence
Run Code Online (Sandbox Code Playgroud)

根据我的猜测,错误是说你不能在同一个数组语句中混合 yaml“映射”和“序列”。

所以它需要是...

stuff:
    thing1: one
    thing2: two
    thing3: 
        - four
Run Code Online (Sandbox Code Playgroud)

或者

stuff:
    thing1: one
    thing2: two
    thing3: three
    thing4: four
Run Code Online (Sandbox Code Playgroud)

取决于您尝试创建的数组类型