Skip to content

Instantly share code, notes, and snippets.

@wouterj
Created February 18, 2014 08:59
Show Gist options
  • Select an option

  • Save wouterj/9067186 to your computer and use it in GitHub Desktop.

Select an option

Save wouterj/9067186 to your computer and use it in GitHub Desktop.
<?php
// ...
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
use Acme\DemoBundle\Entity\Post;
public function indexAction()
{
$post = new Post;
$builder = $this->createFormBuilder($post);
$form = $builder
->add('title', 'text')
->add(
$builder->create('date', 'hidden')
->addViewTransformer(new DateTimeToStringTransformer)
)
->add('submit', 'submit')
->getForm();
$form->handleRequest($this->getRequest());
if ($form->isValid()) {
// ...
return new Response($post->title.' on '.$post->date->format('d-m-Y'));
}
return array('form' => $form->createView());
}
<?php
namespace Acme\DemoBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Post
{
/**
* @var \DateTime
*
* @Assert\Date()
*/
public $date;
public $title;
public function __construct()
{
$this->date = new \DateTime();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment