Last active
August 29, 2015 14:00
-
-
Save ChubV/11348928 to your computer and use it in GitHub Desktop.
This file contains 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 | |
class DateToStringTransformer implements DataTransformerInterface | |
{ | |
/** | |
* @param mixed $value | |
* @return mixed|void | |
*/ | |
public function transform($value) | |
{ | |
if (!$value) { | |
return null; | |
} | |
return $value->format('Y-m-d'); | |
} | |
/** | |
* @param mixed $value | |
* @return mixed|void | |
*/ | |
public function reverseTransform($value) | |
{ | |
return \DateTime::createFromFormat('Y-m-d', $value); | |
} | |
} |
This file contains 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 | |
class SomeType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($builder) { | |
//$game = $event->getData(); | |
$form = $event->getForm(); | |
$period = new \DatePeriod( | |
\DateTime::createFromFormat('Y-m-d', '2014-01-01'), // for test | |
new \DateInterval('P1D'), | |
\DateTime::createFromFormat('Y-m-d', '2014-01-30') // for test | |
); | |
$dates = array(); | |
foreach ($period as $date) { | |
$dates[$date->format("Y-m-d")] = $date->format("j/M"); | |
} | |
$form->add($builder->create('date', 'choice', array( | |
'label' => 'form.game.date', | |
'empty_value' => 'form.game.date', | |
'auto_initialize' => false, | |
'required' => false, | |
'choices' => $dates | |
)) | |
->addModelTransformer(new DateToStringTransformer())->getForm() | |
); | |
}); | |
} | |
public function getName() | |
{ | |
return 'st'; | |
} | |
} |
This file contains 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 | |
class SomeTypeTest extends TypeTestCase | |
{ | |
/** | |
* @test | |
*/ | |
public function testSubmitValidData() | |
{ | |
$begin = new \DateTime(); | |
$formData = array( | |
'date' => '2014-01-15' | |
); | |
$type = new SomeType(); | |
$form = $this->factory->create($type); | |
$form->submit($formData); | |
$this->assertTrue($form->isSynchronized()); | |
$this->assertEquals(['date' => \DateTime::createFromFormat('Y-m-d', '2014-01-15')], $form->getData()); | |
$view = $form->createView(); | |
$children = $view->children; | |
foreach (array_keys($formData) as $key) { | |
$this->assertArrayHasKey($key, $children); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment