Search code examples
ajaxformssymfonyevents

Symfony Form Event POST_SUBMIT not working


I am actually trying to make a post_submit working on a non-entity-mapped form, the goal is to update the content (coming from an API) of a a city select field when i change my zip code input field.

The problem is even if i dump_die a value in the post_submit event it does not show it, like it's never taking in account.

Here is what i have done so far :

My controller :

$form = $this->createForm(EligibilityType::class);

    if($form->isSubmitted() && $form->isValid()){
    }

    return $this->render('eligibility/eligibility.html.twig', [
        'form' => $form->createView(),
    ]);

My form with the events (also where i retrieve api datas) :

->add('postalCode', IntegerType::class, [
            'label' => 'Code postal',
            'attr' => [
                'placeholder' => 'Ex : 75002',
                'class' => 'form-control eligibility_postalcode'
            ]
        ])
        ->add('city', ChoiceType::class, [
            'label' => 'Ville',
            'placeholder' => 'Veuillez saisir un code postal',
            'attr' => [
                'id' => 'eligibility_city'
            ]
        ])

$formModifier = function (FormInterface $form, $postalCode = null) {
        $towns[] = null === $postalCode ? [] : $this->eligibility->getAlphaLinkTown('fr', $postalCode);
        $form->add('city', ChoiceType::class, [
            'placeholder' => 'Sélectionner votre ville...',
            'choices' => $towns,
            'choice_label' => function($choice) {
                return $choice;
            }
        ]);
    };

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function (FormEvent $event) use ($formModifier) {
            $data = $event->getData();
            $formModifier($event->getForm(), $data);
        }
    );

    $builder->get('postalCode')->addEventListener(
        FormEvents::POST_SUBMIT,
        function (FormEvent $event) use ($formModifier) {
            $postalCode = $event->getData();
            $formModifier($event->getForm()->getParent(), $postalCode);
        }
    );
}

My AJAX :

$(document).ready(function() {
let $postalCode = $('#eligibility_postalCode');
$postalCode.on('change', function() {
  let $form = $(this).closest('form');
  let data = {};
  data[$postalCode.attr('name')] = $postalCode.val();
  console.log(data);
  $.ajax({
    url : '/eligibility',
    type: $form.attr('method'),
    data : data,
    success: function(html) {
      console.log(html)
      $('#eligibility_city').replaceWith(
          $(html.responseText).find('#eligibility_city')
      );
    }
  });
});

});

Thank you in advance for all the help you can provide,

Val


Solution

  • $this->handleRequest($request) is missing in your controller. Adding it will allow your controller to handle the difference between GET and POST request. Take some time to read https://symfony.com/doc/current/components/form.html#handling-form-submissions if needed.

    It should help you understand a bit more why your POST_SUBMIT event is not executed by your form.