Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony表单验证日期时间错误_Php_Symfony_Fosrestbundle - Fatal编程技术网

Php Symfony表单验证日期时间错误

Php Symfony表单验证日期时间错误,php,symfony,fosrestbundle,Php,Symfony,Fosrestbundle,当我想用表单将日期时间插入实体“Event”时,它会传递以下错误: 我使用FOS/RestBundle: { code: 400 message: "Validation Failed" -errors: { -children: { name: [ ] description: [ ] -begin: { -errors: [ "This value is not valid." ] -children: { -date: {

当我想用表单将日期时间插入实体“Event”时,它会传递以下错误: 我使用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变量包含以下数组:

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;
    }
}

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

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

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

请问你对此有何看法;提前谢谢