错误再现演示“在映射中不能定义序列项目” [英] Error reproducing demo "You cannot define a sequence item when in a mapping"
问题描述
当我将表单包含在内部时,我可以使表单正常工作的控制器,但是当我将表格形成自己的类时,我最终得到了一个错误。
您在映射时无法定义序列项目500内部
服务器错误 - ParseException
Log返回:
$ b
CRITICAL - 未捕获的PHP异常
Symfony \Component\Yaml\Exception\ParseException:无法在
/ vagrant / vendor / symfony / symfony / src / Symfony / Component / Yaml中映射时定义
序列项/Parser.php
第81行
我似乎无法找到问题所在。
Task.php文件:
<?php
命名空间Acme\TaskBundle\Entity;
类任务
{
保护$任务;
保护$ 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;
$ b DefaultController.php:
<?php
命名空间Acme\TaskBundle\Controller;
使用Symfony \ Bundle \ FrameworkBundle \Controller\Controller;
使用Acme\TaskBundle\Entity\Task;
使用Symfony \Component\HttpFoundation\Request;
使用Acme\TaskBundle\Form\Type\TaskType;
class DefaultController extends Controller
{
public function newAction(Request $ request)
{
//创建任务并为其提供一些虚拟数据这个例子
$ task = new Task();
$ form = $ this-> createForm(new TaskType(),$ task);
$ form-> handleRequest($ request);
if($ form-> isValid()){
//执行一些操作,例如将任务保存到数据库
return $ this- >重定向($这 - > generateUrl( 'task_success'));
$ b $ return $ this-> render('AcmeTaskBundle:Default:new.html.twig',array(
'form'=> $ form-> createView(),
));
$ / code $ / pre
$ p $ TaskType.php:
<?php
// src / Acme / TaskBundle / Form / Type / TaskType.php
命名空间Acme \TaskBundle\Form\Type;
使用Symfony \Component\Form\AbstractType;
使用Symfony \Component\Form\FormBuilderInterface;
使用Symfony \ Component\OptionsResolver\OptionsResolverInterface;
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder-> add('任务')
- > 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',
));
}
}
让我知道你是否需要其他东西。我已经在多个文件中尝试过这种设置。它必须是小事。
$ b
编辑
我拥有的唯一YML文件是我从他们的教程中使用的验证文件。
validation.yml文件
#Acme / TaskBundle / Resources / config / validation.yml
Acme\\ \\ TaskBundle \Entity \任务:
属性:
任务:
- NotBlank:〜
dueDate:
- NotBlank:〜
- 类型: \DateTime
问题是我没有定义数组的yml文件?
解决方案
stuff:
thing1:one //映射
thing2:two //映射
thing3:three //映射
- 四//序列
从我的猜测来看,错误是说你不能混合你的yaml映射和序列。
所以它需要是...
stuff:
thing1:one
thing2:two
thing3:
- four
或
stuff:
thing1:one
thing2:two
thing3:three
thing4:four
取决于您尝试创建的数组类型。
I am getting an error when trying to reproduce a demo that Symfony gives. You can find it here. http://symfony.com/doc/current/book/forms.html#book-form-creating-form-classes
I can get the form to work just fine when I include the form inside of the controller but when I make the form its own class I end up getting an error that says.
You cannot define a sequence item when in a mapping 500 Internal
Server Error - ParseException
Log returns:
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
I can't seem to find where the issue lies.
Task.php File:
<?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;
}
}
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(),
));
}
}
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',
));
}
}
Let me know if you need anything else. I have tried this setup in multiple files. It has to be something small. Its right off of Symfony's site.
Edit
The only YML file I have is the Validation file i used form their tutorial.
validation.yml file
# Acme/TaskBundle/Resources/config/validation.yml
Acme\TaskBundle\Entity\Task:
properties:
task:
- NotBlank: ~
dueDate:
- NotBlank: ~
- Type: \DateTime
Could the problem be that I don't have a yml file that defines an array?
解决方案 Do you have something in one your yml files like this...
stuff:
thing1: one // mapping
thing2: two // mapping
thing3: three // mapping
- four // sequence
From my guess the error is saying that you can't mix your yaml "mapping" and "sequence" in the same array statement.
so it would need to be either...
stuff:
thing1: one
thing2: two
thing3:
- four
or
stuff:
thing1: one
thing2: two
thing3: three
thing4: four
depending on what type of array you were trying to create
这篇关于错误再现演示“在映射中不能定义序列项目”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!