Created
February 18, 2014 08:59
-
-
Save wouterj/9067186 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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