Symfony 4 – Submit form with Axios?

With Axios I get a form created with Symfony FormBuilder, which I then display in a modal:

template.html.twig:

productId = {{product.id}};
            var url = Routing.generate("stripe_admin_product_update_form", {id: productId});
            axios.get(url)
                .then(function(response){
                    var form = response.data.form;
                    Swal.fire({
                        title: "Modifier ?",
                        html: form,
                        icon: "question",
                        showCancelButton: true,
                        allowOutsideClick: () => !Swal.isLoading(),
                        preConfirm: function(result)
                        {
                            //submit form
                        }
                    });
                });

controller.php

 /**
     * @Route("/admin/stripe/product/{id}/update/form", name="stripe_admin_product_update_form", options={"expose"=true})
     *
     */
    public function getUpdateProductForm(StripeProduct $product)
    {
        $form = $this->createFormBuilder($product)
                    ->add("name")
                    ->add("unitLabel")
                    ->add("bankStatementLabel")
                    ->getForm();

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

        $response = [
            "code" => 200,
            "form" => $this->render("stripe_admin/product/_form_edit.html.twig", [
                "form" => $form->createView(),
            ])->getContent()
        ];

        return new JsonResponse($response);
    }

It gives me something like:

enter image description here

How can I now validate the form with Axios?
The goal is therefore to return to the function, and that the line :

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

be detected, so that I can handle any errors and do the rest of the processing

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *