I am trying to read a \DateInterval from 2 input fields, as follows :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, ['label' => "Name"])
->add('duration', DateIntervalType::class, ['label' => false])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => NamedInterval::class]);
}
Here's my DateIntervalType::buildForm :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('amount', IntegerType::class)
->add('kind', ChoiceType::class, ['choices' => ['Year' => 'Y', 'Month' => 'M', 'Week' => 'W']])
;
}
And here is the transformer I attempted :
$builder->get('duration')->addModelTransformer(new CallbackTransformer(
function ($property) {
return new \DateInterval('P' . $property['amount'] . $property['kind']);
},
function ($property) {
/* compute $specString from $property assuming it's a \DateInterval */
/* ... */
return ['amount' => 1, 'kind' => 'W'];
}
));
Hence this is not working, my $property is always null after validating the form when transforming from form data to \DateInterval and I'm not even sure that I ever used the transformer from \DateInterval to form data, what I am doing wrong?