我正在尝试\DateInterval从2个输入字段中读取a ,如下所示:
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]);
}
这是我的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']])
;
}
这是我尝试过的变压器:
$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'];
}
));
因此,这是行不通的,我$property总是null在从表单数据转换到表单数据后验证表单,\DateInterval而我甚至不确定我是否曾经使用过从变压器转换\DateInterval成表单数据,我在做什么错呢?
撒科打诨