M
M
Maxim2015-10-17 20:52:07
symfony
Maxim, 2015-10-17 20:52:07

Using DataTransformer in Symfony2?

Faced with the need to use transformers.
Task: to transform object date/time.
The POST request comes with the date/time as a string.
I know that there is a ready solution from Symfony, namely Symfony\Component\Form\Extension\Core\DataTransfor...
When trying to use it,

use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $transformer = new DateTimeToStringTransformer();
        $builder
            ->add('caption')
            ->add('description')
            ->add($builder->create('startPlan')->addModelTransformer($transformer))
            ->add('owner')
        ;
    }

I face difficulty
Unable to transform value for property path "startPlan": Expected a \DateTime or \DateTimeInterface.

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-10-17
@maxpointn2point

crashes on converting a value from an object to a string, see what you have stored as the value of startPlan

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question