Editor "new" form posts empty values

Editor "new" form posts empty values

com6temcom6tem Posts: 11Questions: 4Answers: 0

Hi everybody,
after one week of search/tests/debugging and headaches... I'm kindly seeking your help on the following problem:
Consider the following simple piece of code (I'm focusing on the POST only):

var editor = new $.fn.dataTable.Editor( {
    ajax: {
        create: {
            type: 'POST',
            url:  '/employees'
        },
        edit: {
            type: 'PUT',
            url:  'XXXXXXXX'
        },
        remove: {
            type: 'DELETE',
            url:  'XXXXXXXX'
        }
    },
    table: "#example",
    fields: [ {
            label: "First name:",
            name: "first_name"
        }, {
            label: "Last name:",
            name: "last_name"
        }, {
            label: "Position:"
            name: "position"
        }
    ]
} );

$('#example').DataTable( {
    dom: "Bfrtip",
    ajax: "/employees",
    columns: [
        { data: "first_name" },
        { data: "last_name" },
        { data: "position" },
    ],
    select: true,
    buttons: [
        { extend: "create", editor: editor },
        { extend: "edit",   editor: editor },
        { extend: "remove", editor: editor }
    ]
} );

==> When I submit the form, it posts empty values for the 3 fields!
Do I need to serialize Editor's form fields?

I'm using Laravel as PHP MVC
The related "#example" datatables displays data correctly, no problem on that.

Thanks a lot for your help!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    Could you link to the page showing the issue so I can debug it please?

    Also, if you use your browser's inspector tools, in the network panel, what does it show was submitted to the server?

    Thanks,
    Allan

  • com6temcom6tem Posts: 11Questions: 4Answers: 0

    Hi Allan,
    I'm sorry I'm still working on a local dev environment, and I thought my question was simple enough for an expert (maybe I was just missing a basic thing...).
    In the meantime, here are some additional details:
    Status code: 500 internal server error
    Server error log says :

    Integrity constraint violation: 1048 Column 'first_name' cannot be null (SQL: insert into `employees` (`first_name`, `last_name`, `position`, `updated_at`, `created_at`) values ( , , , 2015-11-30 05:48:19, 2015-11-30 05:48:19))'
    

    Inspector tools, in the network panel:
    Content type: "text/html; charset=UTF-8"
    action: "create"
    data[0][first_name]: "John"
    data[0][last_name]: "Doe"
    data[0][position]: "Accountant"

    So thanks to your help, I at least now understand that data is sent by the form... but received as empty...

    I see 2 concerns here:
    Content type: shouldn't it be "application/json" instead?
    Data: shouldn't it be first_name: "John" instead of data[0][first_name]: "John"?

    I'm using only the "client side" version of datatables.

    Thanks a lot for your help Allan.

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Content type: shouldn't it be "application/json" instead?

    It might be getting sent as text/html due to the error message. But to be honest, as long as it can be parsed as JSON it doesn't matter too much to jQuery.

    Data: shouldn't it be first_name: "John" instead of data[0][first_name]: "John"?

    No - because of Editor's ability to create multiple rows at the same time, it sends an array of data - see the client / server communication manual.

    So the issue is that the server isn't currently handling the submitted data correctly - is that correct?

    Allan

  • com6temcom6tem Posts: 11Questions: 4Answers: 0

    Hi Allan,
    you are right, I need to handle the data which is sent by Editor.
    I still have a problem with the request being sent form-urlencoded (action=create&data%5B0%5D%5Bfirst_name%5D=John&data%5...), and even option "legacyAjax: true" is not solving my issue...
    But that's another story, I will try to find a solution in this forum's posts.
    Thank you again Allan.

This discussion has been closed.