Symfony – Use javascript in my modal form?

I’m trying to display a modal that contains the content of a form on Symfony.

I call my form with axios and I send it from my controller like this :

    $response = [
        "form" => $this->render("test/coupon/_form_edit_plans.html.twig", [
            "form" => $form->createView(),
        ])->getContent()
    ];

    return new JsonResponse($response);

And my form :

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />

{{form_start(form)}}
{{form_label(form.plans)}} <br>
<div class="custom-control custom-checkbox mb-1 ml-3">
    <input type="checkbox" class="custom-control-input" id="select-all">
    <label class="custom-control-label" for="select-all">Tout sélectionner</label>
</div>
{{form_widget(form.plans)}}
{{form_help(form.plans)}}
{{form_end(form)}}

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>

<script>
    console.log("test");
    $('.plans').select2();

        $("#select-all").click(function(){
            if($("#select-all").is(':checked') ){
                $(".plans > optgroup > option").prop("selected","selected");// Select All Options
                $(".plans").trigger("change");// Trigger change to select 2
            }else{
                $(".plans > optgroup > option").prop("selected", false);
                $(".plans").trigger("change");// Trigger change to select 2
            }
        });
</script>

I manage to display it in my modal window, but the javascript part seems to be ignored. How can I force to accept javascript?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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