重现演示时出错“您无法在映射中定义序列项”

2024-02-26

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

当我将表单包含在控制器中时,我可以让表单正常工作,但是当我将表单设为自己的类时,我最终会收到一条错误消息。

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

日志返回:

严重 - 未捕获的 PHP 异常 Symfony\Component\Yaml\Exception\ParseException:“您不能定义一个 位于映射中时的序列项” /vagrant/vendor/symfony/symfony/src/Symfony/Component/Yaml/Parser.php 81号线

我似乎找不到问题所在。

任务.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;
    }
}

默认控制器.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(),
        ));
    }
}

任务类型.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',
        ));
    }
}

需要帮助请叫我。我已经在多个文件中尝试过此设置。它必须是一些小东西。它就在 Symfony 的网站上。


Edit


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

# Acme/TaskBundle/Resources/config/validation.yml
Acme\TaskBundle\Entity\Task:
    properties:
        task:
            - NotBlank: ~
        dueDate:
            - NotBlank: ~
            - Type: \DateTime

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


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

stuff:
    thing1: one      // mapping
    thing2: two      // mapping
    thing3: three    // mapping
    - four           // sequence

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

所以它需要是...

stuff:
    thing1: one
    thing2: two
    thing3: 
        - four

or

stuff:
    thing1: one
    thing2: two
    thing3: three
    thing4: four

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

重现演示时出错“您无法在映射中定义序列项” 的相关文章