php – Symfony pre_submit更改字段顺序

给定以下表单类型如何在提交之前呈现我的第二个字段?

我尝试使用$form->删除但我总是会收到此错误:

Warning: Illegal offset type in isset or empty

不确定如何解决这个问题.

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('survey', EntityType::class, [
            'class' => SurveyManager::class,
            'attr' => [
                'class' => 'field-change',
            ],
        ])
        ->add('submit', SubmitType::class, [

        ])
        ->addEventListener(
            FormEvents::PRE_SUBMIT,
            function (FormEvent $event) {
                $form = $event->getForm();

                $data = $event->getData();
                $modifier = $data['survey'];
                $form->add('headquarter', EntityType::class, [
                    'class' => HeadQuarterManager::class,
                    'query_builder' => function (HeadQuarterManagerRepository $er) use ($modifier) {
                        return $er->getHeadquarter($modifier);
                    },
                ]);
            }
        );
}

最佳答案 Symfony的Form组件不支持重新排列表单的字段.他们在问题
#5827
#11241上谈到了这个功能.

您可以尝试在EventListener中删除字段提交,然后添加headquerter并提交(是的,再次).但我不确定这是否有效.

另一个解决方案是安装IvoryOrderedFormBundle.它在表单字段中添加了新选项:position.

点赞