class OrderTrackType extends AbstractResourceType {
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('orderItemId', \Symfony\Component\Form\Extension\Core\Type\HiddenType::class,)
->add('trackId', ChoiceType::class, ['choices' => ['UPS'=>1,'USPS'=>2]],
->add('trackNum')
->add('save', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class, ['label' => 'sylius.ui.update',])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => OrderTrack::class,
]);
}
}
在 twig 文件里是 {{ form_widget(form.trackId) }} 设置成 {{ form_widget(form.trackId,{'data':2}) }}没用
第 2 个选项值是 2 , 设置了也还是显示第一个选项
只有在 buildForm 时改成下面这样才行
->add('trackId', ChoiceType::class, ['choices' => ['UPS'=>1,'USPS'=>2], 'data'=>2],
但是 2 这个值在 controller 里才能取到,在 OrderTrackType 里取不到
我现在是在 controller 里 new 这个 form 然后再执行上面这条语句