My Entity:
/**
* @var \DateTime $publishedAt
*
* @ORM\Column(name="published_at", type="date")
*
* @Assert\Date()
*/
private $publishedAt;
/**
* Set publishedAt
*
* @param \DateTime $publishedAt
* @return MagazineIssue
*/
public function setPublishedAt(\DateTime $publishedAt)
{
$this->publishedAt = $publishedAt;
return $this;
}
/**
* Get published_at
*
* @return \DateTime
*/
public function getPublishedAt()
{
return $this->publishedAt;
}
My form builder:
$builder->add('publishedAt');
My view:
{{ form_widget(form) }}
When I select the date in the selects and submit the form I catche the error:
Catchable Fatal Error: Argument 1 passed to ... must be an instance of DateTime,
string given, called in .../vendor/symfony/symfony/src/Symfony/Component/Form
/Util/PropertyPath.php on line 537 and defined in ... line 214
Why it happens? If I replace the field setter with public function setPublishedAt($publishedAt) I got the error:
Fatal error: Call to a member function format() on a non-object
in .../vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateType.php on line 44
If I change the form builder to
$builder->add('publishedAt','date')
all works fine. Why it happens? Why symfony can’t guess it and pass to field setter the proper date format (\DateTime instead of string)?
EDIT: if I remove the @Assert\Date() then all works fine too. I think it’s a sf2.1 bug with guessing the date field type
To solve this problem you can
1.change the assert from
@Assert\Date()to@Assert\Type('\DateTime')OR
2.change the form builder to
$builder->add('publishedAt','date')OR
3.specify the
inputoption in the form builder:$builder->add('publishedAt',null,array('input' => 'datetime'))