Symfony表格验证日期时间错误

时间:2014-07-28 13:22:03

标签: php symfony fosrestbundle

当我想使用表单将Datetime插入我的实体'事件'时,它会发送以下错误: 我使用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一起发生吗?

2 个答案:

答案 0 :(得分:0)

您应该使用\ DateTime在字符串中提供日期。确保在保持原则之前(可能在验证之前)转换“开始”和“结束”值。

答案 1 :(得分:0)

默认日期时间字段需要以下结构:

"datePicture": {
    "date": {
        "year": 2014,
        "month": 11,
        "day": 5
    },
    "time": {
        "hour": 23,
        "minute": 11
    }
}