Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony pre_submit change field order

Given the following form type how can I render my second field before submit? I tried with $form->remove but I would always get this error:

Warning: Illegal offset type in isset or empty

Not really sure how to fix this one.

/**
 * @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);
                    },
                ]);
            }
        );
}
like image 287
L.S Avatar asked Nov 08 '22 07:11

L.S


1 Answers

the solution that i used was to remove the submit button from class type and manually add it in the template then use {{ form_rest(form) }} before the submit button and it will render it ok.

like image 193
L.S Avatar answered Nov 14 '22 21:11

L.S