0

I was posting a Symfony form via AJAX post and after I made a field edit on my "Siparis" entity and related form field in "SiparisType" I keep getting 419 on post.

Symfony version: 6.0.2 - PHP version:8.1.2 Database: 10.4.22-MariaDB

I tried to disable csrf protection and still getting the same status 419 and nothing else. Symfony route to handle AJAX post(I reverted my latest changes after editing the field below version was working fine earlier):

#[Route('/save/', name: 'save', methods: ['POST'])]
public function save_siparis(Request $request, EntityManagerInterface $entityManager): JsonResponse
{
    $siparis = new Siparis();
    try {
        $form = $this->createForm(SiparisType::class, $siparis);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->persist($siparis);
            $entityManager->flush();

            return new JsonResponse(['result' => 'OK', 'siparis_id' => $siparis->getId()]);
        }
    }catch(\Exception $e){
        error_log($e->getMessage());
        return new JsonResponse(array('result' => $e->getMessage()), 419);
    }
}

Render Form route on Controller: Only change is: 'firma' => $id option for form in this section

#[Route('/new/{id}', name: 'new', methods: ['GET'])]
public function new($id, Request $request, EntityManagerInterface $entityManager, SiparisRepository $siparisRepository,FirmaRepository $firmaRepository, IlgiliRepository $ilgiliRepository): Response
{
    $siparis = new Siparis();
    $latest_siparis = $siparisRepository->findBy(['Musteri' => $id], ['id' => 'desc', ]);
    $siparis_id = 1;
    if(!is_null($latest_siparis))
        $siparis_id = count($latest_siparis) + 1;


    $musteri = $firmaRepository->find($id);
    $siparisNo = 'TST-'.str_replace(' ', '-',$musteri->getKod()).'-'.str_pad($siparis_id, 3, '0', STR_PAD_LEFT);
    $form = $this->createForm(SiparisType::class, $siparis, ['firma' => $id,'attr' => ['id' => 'siparis_form']]);
    $firmalar = $firmaRepository->findBy(['Type' => 0]);
    $maliyet = new Maliyetler();
    $maliyet_form = $this->createForm(MaliyetlerType::class, $maliyet, ['attr' => ['id' => 'maliyet_form']]);
    
    return $this->renderForm('siparis/new.html.twig', [
        'siparisNo' => $siparisNo,
        'sipari' => $siparis,
        'form' => $form,
        'maliyet_form' => $maliyet_form,
        'satici_firmalar' => $firmalar,
        'musteri' => $musteri,
        'mode' =>'NEW'
    ]);
}

My FormType: The field: 'siparis_veren' was a TextType and i have changed this one as a EntityType and fetched the related entities via query builder

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $firma = $options['firma'];
    $SIPARIS_CHOICES = ['Devam Ediyor' => '1','Tamamlandı' => '2','İptal' => '3'];
    $builder
        ->add('teslim_tarihi', DateType::class, [
            'widget' => 'single_text',
            'label' => 'Teslim Tarihi', 'attr' =>['class' => 'form-control', 'placeholder' => 'Teslim Tarihi']
        ])
        ->add('siparis_tarihi', DateType::class, [
            'widget' => 'single_text',
            'label' => 'Sipariş Tarihi', 'attr' =>['class' => 'form-control', 'placeholder' => 'Sipariş Tarihi']
        ])
        ->add('siparis_veren', EntityType::class,['class' => Ilgili::class,
            'choice_label' => 'full_name',
            'query_builder' => function (EntityRepository $er) use ($firma) {
                return $er->createQueryBuilder('ilgili')
                    ->andWhere('ilgili.firma = :firma')
                    ->setParameter('firma', $firma);
            },
            'label' => 'Siparişi Veren', 'attr' =>['class' => 'form-control', 'placeholder' => 'Siparişi Veren']])
        ->add('siparis_durum', ChoiceType::class,['choices' => $SIPARIS_CHOICES,'attr' =>['class' => 'form-control', 'placeholder' => 'Siparişi Alan'] ])
        ->add('siparis_turu', HiddenType::class)
        ->add('siparis_genel_aciklama', TextType::class,['label' => 'Sipariş Genel Açıklaması', 'attr' =>['class' => 'form-control', 'placeholder' => 'Sipariş Genel Açıklaması']])
        ->add('siparis_satir_aciklama', HiddenType::class)
        ->add('siparis_miktari', HiddenType::class)
        ->add('siparis_fiyati', HiddenType::class)
        ->add('fatura_durumu', HiddenType::class)
        ->add('siparisNo', HiddenType::class)
        ->add('siparis_kdv_orani', HiddenType::class);
}

public function configureOptions(OptionsResolver $resolver): void
{
    $resolver->setDefaults([
        'data_class' => Siparis::class,
        'csrf_protection' => true,
        'csrf_field_name' => '_token',
    ]);
    $resolver->setRequired(['firma']);
}

My AJAX form post on client side(No change here):

$('form[name="siparis"]').submit(function(e) {
        e.preventDefault();

        var url = "{{ path('siparis_save') }}";
        var formSerialize = $(this).serialize();
        $.post(url, formSerialize, function(response) {
            if(response.result === "OK")
            {
                $( 'form[name="maliyetler"]' ).submit();
            }
            else
            {
                console.log(response.result);
            }

        }, 'JSON');
    });

In my Siparis Entity siparis_veren was a text field and i added a ManyToOne relation with Ilgili class to it and after my changes post keep giving me 419 error.

#[ORM\ManyToOne(targetEntity: Ilgili::class, inversedBy: 'siparisler')]
#[ORM\JoinColumn(nullable: false)]
private $siparis_veren;

I tried clearing cache, removed related siparis_veren field from form type and controller but result is same unless I revert siparis_veren to TextType as it was before.

Sorry if my explanation is not enough, I couldn't find any results regarding this issue and SO is my only choice, I am a newbie in Symfony.

Thank you.

Futi7
  • 31
  • 9
  • 1
    What's the return of your Ajax request ? Cause it's seems that your try catch catch an error ? – jean-max Jan 30 '22 at 16:56
  • There are no response at all I am tracking the request from Symfony AJAX Request from debug bar, no error on there too. – Futi7 Jan 30 '22 at 17:12
  • 1
    Your try catch in the function `save_siparis` doesn't catch anything ? Cause your error 419 comes from `return new JsonResponse(array('result' => $e->getMessage()), 419);` I supposed. If it's that, you should look at the message. Or look at this post to get the error message from post request : https://stackoverflow.com/questions/2833951/how-do-i-catch-an-ajax-query-post-error – jean-max Jan 30 '22 at 17:41
  • 1
    I set a breakpoint in ajax response handling but somehow it never triggered, I removed status 419 and checked again after your advise and found the issue, thank you for your help – Futi7 Jan 30 '22 at 17:51

1 Answers1

1

Okay found the issue, I didn't notice that I set status to 419 on Try Catch catch block so thought it was about Symfony or client side. After doing some debugging I get "An error has occurred resolving the options of the form "App\Form\SiparisType": The required option "firma" is missing." error which is caused by

SiparisConroller.php

$form = $this->createForm(SiparisType::class, $siparis);

This line in save_siparis() function. In SiparisType I have set 'firma' field as required SiparisType.php

$resolver->setRequired(['firma']);

Therefore I got an error in Try Catch block.

Thanks to @jean-max for his advice to check Try Catch.

Futi7
  • 31
  • 9