I’m in the need of a custom field that accepts either a string or a DateTime object as input. After some discussion I decided that DataTransformers are the way to go. I followed the instructions on the Cookbook, but I’m getting the follwing error:
Catchable Fatal Error: Argument 1 passed to
Symfony\Component\Form\FormFactory::loadTypeExtensions() must
implement interface Symfony\Component\Form\FormTypeInterface, instance
of Yanic\HomeBundle\Form\DataTransformer\DateToStringTransformer
given, called in
/Applications/MAMP/htdocs/symfony-standard-2.1/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php on line 320 and defined in
/Applications/MAMP/htdocs/symfony-standard-2.1/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 332
It seems that it is expecting a FormType, but the docs says that shouldn’t be necessary.
That’s the code I’m using:
UsersType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new \Yanic\HomeBundle\Form\DataTransformer\DateToStringTransformer();
$builder
->add('username')
->add('password','password',
array(
'required' => false
))
->add('email')
->addViewTransformer( $transformer )
->add('created', 'datetimeToString', array(
'disabled' => true,
))
->add('modified', 'datetimeToString', array(
'disabled' => true,
))
->add('isActive', null, array(
'label' => 'Is active?',
'required' => false
))
->add('lastLogin', null, array(
'empty_data' => 'never',
'widget' => 'single_text',
'read_only' => true,
'label' => 'Last login'
))
->add('modifiedBy', null, array(
'label' => 'Modified by',
'read_only' => true
))
->add('groups')
;
}
DateToStringTransformer.php
namespace Yanic\HomeBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class DateToStringTransformer implements DataTransformerInterface
{
/**
* Transforms an DateTime object to a string.
*
* @param \DateTime|null $issue
* @return string
*/
public function transform($date)
{
if (null === $date) {
return "never";
}
return $date->format('d-m-Y H:i');
}
/**
* Transforms a string (date formatted) to an object (\DateTime).
*
* @param string $date
* @return \DateTime|null
* @throws TransformationFailedException
*/
public function reverseTransform($number)
{
return $number;
}
}
services.yml
services:
form.dataTransformer.datetimeToString:
class: Yanic\HomeBundle\Form\DataTransformer\DateToStringTransformer
tags:
- { name: form.type, alias: datetimeToString }
What am I doing wrong?
Ah, I’m using Symfony 2.1 Beta2
In documentation is hint about it:
“use a normal text field, but transform the text into an issue object”**
You registered data transformer as form type in the service.
Symfony treat you data transformer as form type then.
You should create form type and put your data transformer there (like in doc), or remove service definition, use normal date field instead of datetimeToString.
But I think in that case better will be first way.