EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.04k stars 1.02k forks source link

AjaxEdit function doesn't allow refreshing edit dynamic form #3764

Open eotin opened 4 years ago

eotin commented 4 years ago

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.

PopePAF commented 2 years ago

I have this issue too... I found https://github.com/EasyCorp/EasyAdminBundle/issues/581 so far, where I found https://github.com/EasyCorp/EasyAdminBundle/issues/581#issuecomment-158626018 But the method is still there... and it would always transform the new value to a bool...

If I interpret that correctly, this ajaxEdit is only ment to be used for those checkbox boolean values within the index views for example... Other use cases might not be supported yet...

eotin commented 1 month ago

Any news on this one ? Seems the problem is still here in v4