当我想插入一个日期时间到我的实体'事件'与表单它提供以下错误:我使用FOS/RestBundle:
{
code: 400
message: "Validation Failed"
-errors: {
-children: {
name: [ ]
description: [ ]
-begin: {
-errors: [ "This value is not valid." ]
-children: {
-date: {
-children: {
year: [ ]
month: [ ]
day: [ ]
}
}
-time: {
-children: {
hour: [ ]
minute: [ ]
}
}
}
}
-end: {
-errors: [
"This value is not valid."
]
-children: {
-date: {
-children: {
year: [ ]
month: [ ]
day: [ ]
}
}
-time: {
-children: {
hour: [ ]
minute: [ ]
}
}
}
}
admins: [ ]
}
}
}
这是我处理请求的地方:
public function post(array $parameters){
return $this->processForm(new Event, $parameters, 'POST');
}
private function processForm(Event $event, array $parameters, $method = "PUT"){
$form = $this->formFactory->create( new EventType(), $event, array('method' => $method));
$form->submit($parameters, 'PATCH' !== $method );
if($form->isValid()){
$event = $form->getData();
$this->om->persist($event);
$this->om->flush($event);
return $event;
}
throw new InvalidFormException('Invalid submitted data', $form);
}
$parameters var包含以下数组:
array(4) {
["name"]=>
string(8) "Dogevent"
["description"]=>
string(13) "Wow such Vote"
["begin"]=>
string(19) "2014-05-30 21:10:00"
["end"]=>
string(19) "2014-05-30 22:10:00"
}
实体事件描述如下:
<?php
namespace Antenne'VoteBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
* Event
*
* @ORM'Table()
* @ORM'Entity(repositoryClass="Antenne'VoteBundle'Entity'EventRepository")
*/
class Event
{
/**
* @var integer
*
* @ORM'Column(name="id", type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM'Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM'Column(name="description", type="text")
*/
private $description;
/**
* @var 'DateTime
*
* @ORM'Column(name="begin", type="datetime")
*/
private $begin;
/**
* @var 'DateTime
*
* @ORM'Column(name="end", type="datetime")
*/
private $end;
/**
* @var Song $songs
*
* @ORM'OneToMany(targetEntity="Song", mappedBy="event")
*/
private $songs;
/**
* @var User $admins
*
* @ORM'ManyToMany(targetEntity="User")
*/
private $admins;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Event
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
* @return Event
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set begin
*
* @param 'DateTime $begin
* @return Event
*/
public function setBegin($begin)
{
$this->begin = $begin;
return $this;
}
/**
* Get begin
*
* @return 'DateTime
*/
public function getBegin()
{
return $this->begin;
}
/**
* Set end
*
* @param 'DateTime $end
* @return Event
*/
public function setEnd($end)
{
$this->end = $end;
return $this;
}
/**
* Get end
*
* @return 'DateTime
*/
public function getEnd()
{
return $this->end;
}
/**
* Constructor
*/
public function __construct()
{
$this->songs = new 'Doctrine'Common'Collections'ArrayCollection();
$this->admins = new 'Doctrine'Common'Collections'ArrayCollection();
}
/**
* Add songs
*
* @param 'Antenne'VoteBundle'Entity'Song $songs
* @return Event
*/
public function addSong('Antenne'VoteBundle'Entity'Song $songs)
{
$this->songs[] = $songs;
return $this;
}
/**
* Remove songs
*
* @param 'Antenne'VoteBundle'Entity'Song $songs
*/
public function removeSong('Antenne'VoteBundle'Entity'Song $songs)
{
$this->songs->removeElement($songs);
}
/**
* Get songs
*
* @return 'Doctrine'Common'Collections'Collection
*/
public function getSongs()
{
return $this->songs;
}
/**
* Add admins
*
* @param 'Antenne'VoteBundle'Entity'User $admins
* @return Event
*/
public function addAdmin('Antenne'VoteBundle'Entity'User $admins)
{
$this->admins[] = $admins;
return $this;
}
/**
* Remove admins
*
* @param 'Antenne'VoteBundle'Entity'User $admins
*/
public function removeAdmin('Antenne'VoteBundle'Entity'User $admins)
{
$this->admins->removeElement($admins);
}
/**
* Get admins
*
* @return 'Doctrine'Common'Collections'Collection
*/
public function getAdmins()
{
return $this->admins;
}
}
即使我删除了所有验证,它仍然打印出这个错误。
有办法修复这个错误吗?它会在与FOSRestBundle的连接中发生吗?
默认的datetime字段的结构如下:
"datePicture": {
"date": {
"year": 2014,
"month": 11,
"day": 5
},
"time": {
"hour": 23,
"minute": 11
}
}
当您应该使用'DateTime时,您以字符串形式提供日期。确保在坚持原则之前(可能在验证之前)转换了"begin"answers"end"值。