gpt4 book ai didi

php - 复制演示 "You cannot define a sequence item when in a mapping"时出错

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:38:47 33 4
gpt4 key购买 nike

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

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

You cannot define a sequence item when in a mapping 500 Internal Server Error - ParseException

日志返回:

CRITICAL - Uncaught PHP Exception Symfony\Component\Yaml\Exception\ParseException: "You cannot define a sequence item when in a mapping" at /vagrant/vendor/symfony/symfony/src/Symfony/Component/Yaml/Parser.php line 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;
}
}

默认 Controller .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 的网站上。


编辑


我拥有的唯一 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

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

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

关于php - 复制演示 "You cannot define a sequence item when in a mapping"时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18750417/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com