sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Form field do not update after insert (display JSON response in modal) #1815

Closed cbourgois closed 10 years ago

cbourgois commented 10 years ago

I have an admin with a field defined like this:

->add('file', 'sonata_type_model_list', 
    array(
        'required' => true
    ), 
    array(
        'link_parameters' => array(
            'context' => 'documentations',
            'provider' => 'sonata.media.provider.file'
        )
    )
)

When I click to add file, a model opens, I add a file and send the form, it displays a JSON response in the dialog:

{"result":"ok","objectId":"237"}    

The console output is :

[Sonata.Admin] [s5293847238d1f_file|field_dialog_form_add] add link action base.js:53
XHR finished loading: "http://localhost:8000/fr/admin/sonata/media/media/create?provider=sonata.me…dia&pcode=project.products.admin.documentation&puniqid=s5293847238d1f". jquery-1.8.3.js:8434
[Sonata.Admin] [s5293847238d1f_file|field_dialog_form_add] ajax success, [object Object] base.js:53
[Sonata.Admin] [s5293847238d1f_file|field_dialog_form_action] action catch, [object HTMLFormElement] base.js:53
[Sonata.Admin] [s5293847238d1f_file|field_dialog_form_action] execute ajax call base.js:53
XHR finished loading: "http://localhost:8000/fr/admin/sonata/media/media/create?provider=sonata.media.provider.file&context=documentations&uniqid=s5293847259dec". jquery-1.8.3.js:8434
[Sonata.Admin] [s5293847238d1f_file|field_dialog_form_action] ajax success base.js:53

It seems that response is considered as string (this part of auto generated code is not executed) :

if (typeof data != 'string' && data.result == 'ok') {
    field_dialog_s5293847238d1f_file.dialog('close');

    jQuery('#s5293847238d1f_file').val(data.objectId);
    jQuery('#s5293847238d1f_file').change();

    return;
}

This bug is similar to sonata-project/SonataAdminBundle#247, but solution isn't appropriate (my entity has a setFile function)

Any Idea ?

sroze commented 10 years ago

If you put a breakpoint on the line bellow, what is the value of data ? if (typeof data != 'string' && data.result == 'ok') {

cbourgois commented 10 years ago

I've added a debugger; before if test.

The Local Scope contains:

data : "{"result":"ok","objectId":"240"}"
this: Object

So data contains:

{"result":"ok","objectId":"240"}

The response Header of /sonata/media/media/create?provider=sonata.media.provider.file&context=documentations&uniqid=s52947db431bf7 is

Cache-Control:private
Connection:close
Content-Type:text/plain; charset=UTF-8
Date:Tue, 26 Nov 2013 10:53:59 GMT
Host:localhost:8000
X-Debug-Token:539be8
X-Powered-By:PHP/5.4.4

Is it normal that response is in text/plain ?

rande commented 10 years ago

No it is not normal ...

Thomas Rabaix http://rabaix.net - http://sonata-project.org On Nov 26, 2013 11:59 AM, "Cyrille Bourgois" notifications@github.com wrote:

I've added a debugger; before if test.

The Local Scope contains:

data : "{"result":"ok","objectId":"240"}" this: Object

So data contains:

{"result":"ok","objectId":"240"}

The response Header of /sonata/media/media/create?provider=sonata.media.provider.file&context=documentations&uniqid=s52947db431bf7is

Cache-Control:private Connection:close Content-Type:text/plain; charset=UTF-8 Date:Tue, 26 Nov 2013 10:53:59 GMT Host:localhost:8000 X-Debug-Token:539be8 X-Powered-By:PHP/5.4.4

Is it normal that response is in text/plain ?

— Reply to this email directly or view it on GitHubhttps://github.com/sonata-project/SonataAdminBundle/issues/1815#issuecomment-29283428 .

cbourgois commented 10 years ago

Ok, I found that if I change code below in /vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Controller/CRUDController.php it works.

Original :

public function renderJson($data, $status = 200, $headers = array())
{
    // fake content-type so browser does not show the download popup when this
    // response is rendered through an iframe (used by the jquery.form.js plugin)
    //  => don't know yet if it is the best solution
    if ($this->get('request')->get('_xml_http_request')
        && strpos($this->get('request')->headers->get('Content-Type'), 'multipart/form-data') === 0) {
        $headers['Content-Type'] = 'text/plain';
    } else {
        $headers['Content-Type'] = 'application/json';
    }

    return new Response(json_encode($data), $status, $headers);
}

Modified version (force Response Content-Type to application/json):

public function renderJson($data, $status = 200, $headers = array())
{
    // fake content-type so browser does not show the download popup when this
    // response is rendered through an iframe (used by the jquery.form.js plugin)
    //  => don't know yet if it is the best solution
    if ($this->get('request')->get('_xml_http_request')
        && strpos($this->get('request')->headers->get('Content-Type'), 'multipart/form-data') === 0) {
        $headers['Content-Type'] = 'application/json';
    } else {
        $headers['Content-Type'] = 'application/json';
    }

    return new Response(json_encode($data), $status, $headers);
}

Any Idea ?

sroze commented 10 years ago

Which version (the commit SHA is the best) of SonataAdminBundle are you using ?

cbourgois commented 10 years ago

I have defined in my composer.json :

"sonata-project/admin-bundle": "dev-master"

In my composer.lock I read :

{
    "name": "sonata-project/admin-bundle",
    "version": "dev-master",
    "target-dir": "Sonata/AdminBundle",
    "source": {
        "type": "git",
        "url": "https://github.com/sonata-project/SonataAdminBundle.git",
        "reference": "d02b7e0b5246556c338ba004050db4f456f06690"
    },
    "dist": {
        "type": "zip",
        "url": "https://api.github.com/repos/sonata-project/SonataAdminBundle/zipball/d02b7e0b5246556c338ba004050db4f456f06690",
        "reference": "d02b7e0b5246556c338ba004050db4f456f06690",
        "shasum": ""
    },
    "require": {
        "doctrine/common": "~2.2",
        "knplabs/knp-menu-bundle": "~1.1",
        "sensio/generator-bundle": "~2.2",
        "sonata-project/block-bundle": "~2.2,>=2.2.7",
        "sonata-project/exporter": "1.*",
        "sonata-project/jquery-bundle": "1.8.*",
        "symfony/class-loader": "~2.2",
        "symfony/config": "~2.2",
        "symfony/console": "~2.2",
        "symfony/form": "~2.2",
        "symfony/http-foundation": "~2.2",
        "symfony/routing": "~2.2",
        "symfony/security-bundle": "~2.2",
        "symfony/twig-bridge": "~2.2",
        "symfony/validator": "~2.2",
        "twig/extensions": "~1.0",
        "twig/twig": "~1.12"
    },
    "require-dev": {
        "jms/translation-bundle": "*@dev",
        "sonata-project/intl-bundle": "~2.1",
        "symfony/yaml": "~2.2"
    },
    "suggest": {
        "sonata-project/doctrine-orm-admin-bundle": "2.2.*@dev",
        "sonata-project/intl-bundle": "2.1.*"
    },
    "type": "symfony-bundle",
    "extra": {
        "branch-alias": {
            "dev-master": "2.2.x-dev"
        }
    },
    "autoload": {
        "psr-0": {
            "Sonata\\AdminBundle": ""
        }
    },
    "notification-url": "https://packagist.org/downloads/",
    "license": [
        "MIT"
    ],
    "authors": [
        {
            "name": "Thomas Rabaix",
            "email": "thomas.rabaix@sonata-project.org",
            "homepage": "http://sonata-project.org"
        },
        {
            "name": "Sonata Community",
            "homepage": "https://github.com/sonata-project/SonataAdminBundle/contributors"
        }
    ],
    "description": "Symfony SonataAdminBundle",
    "homepage": "http://sonata-project.org/bundles/admin",
    "keywords": [
        "Admin Generator",
        "admin",
        "bootstrap",
        "sonata"
    ],
    "time": "2013-11-25 11:47:35"
},

Where I can find the sha1 ?

sroze commented 10 years ago

It's the in source.reference, the SHA1 is d02b7e0b5246556c338ba004050db4f456f06690, it's the current master of SonataAdminBundle. I'll have a look, it was working on previous releases...

kepeket commented 10 years ago

Hi,

any update? Will it be available again in HEAD?

rande commented 10 years ago

I have reverted the code, so this should work fine now.

ping me if not