I recently build a DataTransformer
that will accept a variety of different date formats in the end (like 12/12/2012 or 12.12.2012 or 2012-12-12).
My question is: before I had the date and time split up into two different fields:
$builder->add('date_end', 'datetime', array(
'label' => 'Date/Time',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'date_format' => 'dd.MM.yyyy',
'with_seconds' => false,
'required' => false,
) )
Which worked fine. How can I accomplish this with the new DataTransformer
?
Within my entity-type:
public function buildForm(FormBuilder $builder, array $options) {
builder->add('date_end', 'dateTime', array(
'label' => 'Date/Time',
'required' => false,
) )
}
The corresponding DateTimeType I created:
class DateTimeType extends AbstractType {
private $om;
public function __construct(ObjectManager $om) {
$this->om = $om;
}
public function buildForm(FormBuilder $builder, array $options) {
$transformer = new DateTimeTransformer($this->om);
$builder->appendClientTransformer($transformer);
}
public function getDefaultOptions(array $options) {
return array(
'invalid_message' => 'TODO INVALID',
);
}
public function getParent(array $options) {
return 'text';
}
public function getName(){
return 'dateTime';
}
}
My transformer so far:
/**
* @param \DateTime|null $dateTime
* @return string|null
*/
public function transform($dateTime) {
if (null === $dateTime) {
return null;
}
return $dateTime->format('Y-m-d H:i');
}
/**
* @param string $value
* @return string|null
*/
public function reverseTransform($value)
{
if ( (null === $value) || empty($value) ) {
return null;
}
return new \DateTime( $value );
}