EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

AjaxEdit function doesn't allow refreshing edit dynamic form

Open eotin opened this issue 4 years ago • 2 comments

Describe the bug

As explained here https://symfony.com/doc/current/form/dynamic_form_modification.html

I used to use an ajax function on my edit form to refresh items in a list depending another one on the page. I'm using the $builder->addEventListener(FormEvents::PRE_SET_DATA, and $builder->addEventListener( FormEvents::SUBMIT,

Then i need a javascript part to submit the form and replace the items when getting the response :

<script>
    var $changeFunction = function() {
        myModal.showPleaseWait();
        debugger;
        var $form = $(this).closest('form');
        var data = {};
        data[$('#Permission_resource').attr('name')] = $('#Permission_resource').val();
        // Submit data via AJAX to the form's action path.
        $.ajax({
            url : $form.attr('action'),
            type: $form.attr('method'),
            data : data,
            success: function(html) {
                //debugger;

                if ($(html).find('#Permission_action')) {
                    // Replace current position field ...
                    $('#Permission_action').replaceWith(
                        // ... with the returned one from the AJAX response.
                        $(html).find('#Permission_action')
                    );
                }

On the new form, there is no issue as there is no ajaxEdit function called in the new function of the CrudController. But on the edit form, it generates an error because of this part of code :

 if ($context->getRequest()->isXmlHttpRequest()) {
            $fieldName = $context->getRequest()->query->get('fieldName');
            $newValue = 'true' === mb_strtolower($context->getRequest()->query->get('newValue'));

            $event = $this->ajaxEdit($context->getEntity(), $fieldName, $newValue);
            if ($event->isPropagationStopped()) {
                return $event->getResponse();
            }

            // cast to integer instead of string to avoid sending empty responses for 'false'
            return new Response((int) $newValue);
        }


What is the best way to keep this fonctionnality on the edit form of the bundle ?

Thanks

To Reproduce Steps to reproduce this error and also, the EasyAdmin version used.

(OPTIONAL) Additional context If they are useful, include logs, code samples, screenshots, etc.

eotin avatar Sep 15 '20 12:09 eotin