fsteinbauer
fsteinbauer

Reputation: 161

Symfony Form validates Datetime wrong

When I want to insert a Datetime into my Entity 'Event' with a Form it delivers following Error: I use the 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: [ ]
}
}
}

This is where I process the request:

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

The $parameters var contains following array:

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

The Entity Event is described by:

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

Even when I remove all validations, it still prints out this error.

Is there a way to fix this Error? Can it occur in connection with FOSRestBundle?

Upvotes: 0

Views: 1173

Answers (2)

M. Hanafi
M. Hanafi

Reputation: 66

A default datetime field expects the following structure:

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

Upvotes: 1

piotr.jura
piotr.jura

Reputation: 899

You provide dates in strings, when you should use \DateTime. Make sure you convert "begin" and "end" values before persisting in doctrine (and possibly before validating).

Upvotes: 0

Related Questions