我在尝试重现Symfony提供的演示时遇到错误。 你可以在这里找到它。 http://symfony.com/doc/current/book/forms.html#book-form-creating-form-classes
当我在控制器中包含表单时,我可以让表单正常工作,但是当我使表单成为自己的类时,我最终会收到一个错误。
在映射 500 内部时,无法定义序列项 服务器错误 - 分析异常
日志返回:
严重 - 未捕获的 PHP 异常 Symfony\Component\Yaml\Exception\ParseException:"你不能定义一个 映射中的序列项"在 /vagrant/vendor/symfony/symfony/src/Symfony/Component/Yaml/Parser.php 81路
我似乎找不到问题出在哪里。
任务.php文件:
<?php
namespace AcmeTaskBundleEntity;
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 AcmeTaskBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use AcmeTaskBundleEntityTask;
use SymfonyComponentHttpFoundationRequest;
use AcmeTaskBundleFormTypeTaskType;
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 AcmeTaskBundleFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
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' => 'AcmeTaskBundleEntityTask',
));
}
}
如果您需要其他任何东西,请告诉我。 我已经在多个文件中尝试过此设置。 它必须是小东西。 它就在Symfony的网站旁边。
编辑
我唯一的YML文件是我在他们的教程中使用的验证文件。验证.yml 文件
# Acme/TaskBundle/Resources/config/validation.yml
AcmeTaskBundleEntityTask:
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
取决于您尝试创建的数组类型
在您的类型中,将->add('dueDate', null, array('mapped' => false))
行更改为->add('dueDate', null, array())
您的dueDate
确实已映射