DataTables Editor Checkbox sends boolean value as a string to the server.

DataTables Editor Checkbox sends boolean value as a string to the server.

washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

I have three fields as follows:

{
    "name": "IsDB",
    "label": "",
    "className": "",
    "type": "checkbox",
    options: [
        { label: "Database", value: 1 }
    ],
    separator: '',
    unselectedValue: 0
},
{
    "name": "IsWebsite",
    "label": "",
    "className": "",
    "type": "checkbox",
    options: [
        { label: "Website", value: "false" }
    ],
    separator: '',
    unselectedValue: "false"
},
{
    "name": "IsOtherThingy",
    "label": "",
    "className": "",
    "type": "checkbox",
    options: [
        { label: "Thingy", value: true }
    ],
    separator: '',
    unselectedValue: false
},

Every single one of those will submit a JSON payload to the server with a value of "false" or "true" and never the actual boolean of false and true.

Is there a flag to say "do not coerce this checkbox value to a string"?

Answers

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited April 2020

    You can't submit boolean values from Javascript - at least not to PHP. It is not going to work. You can do that from PHP to Javascript but not the other way round.

    Of course you can submit strings like "true" or "false". No problem, but not true or false as boolean.

    Just submit 0 and 1 please and cast them as boolean in PHP. Better than checking for "true" and "false" as strings.

    Like in here:

    if ( (bool)filter_input(INPUT_POST, 'yourPostedValue') ) {
       do something ...
    

    This is not related to Data Tables or Editor; it is more of a Javascript and / or PHP issue.

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    I assumed sending { "MyBool": true } was valid JSON.

    DataTables should not coerce bools to strings just to cater to PHP. That is silly.

    I am using ASPNET Core. I realize I can override all of this in the editor REST methods but my code is highly dynamic so I avoid doing that whenever possible.

    I have just removed all checkboxes and changed them to select dropdowns which submit true/false as normal JavaScript bools.

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    I don't know whether or not Data Tables "coerces" boolean variables to string variables ... @allan or @colin can you comment?

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    @rf1234

    I am seeing this behavior all over DTE actually. Try to set a field input attr type to number. It still sends { "myField": "123" } instead of { "myField": 123 }

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    There are a couple of things going on here:

    1. When a value is read from an <input> element in the document it is always a string. That said, Editor's select, radio and checkbox inputs have code to preserve static typing. So what I think you are seeing is:
    2. When the data is submitted to the server it is sent as http parameter - which are type-less.

    This is how our .NET libraries had the type conversion. Are you using our libraries, or your own code for this?

    Allan

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    @allan I am using the editor with my own REST API, I JSON.stringify and send it.

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    @allan Also, checkbox field preserves static typing? Is there something wrong with my examples above? None of them send true. They all send "true".

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    They send "true" because it is sending http parameters - and as I mentioned that it untyped (so the server is presumably always reading them as a string).

    You could send the data as JSON as described in the last example in the ajax.data docs which would preserve the boolean type, or you can coerce the type on the server-side, similar to what our libraries do.

    Allan

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    @allan Reproduction: http://live.datatables.net/bejemebi/1/ see console.

    Following the JSOn example as shown in ajax.data did not preserve the boolean type. It looks like no matter what, its always a string.

    Can you please advise if I did something incorrect in that snippet?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    No I agree. There is something going wrong there. I'll look into it once I've finished my other support rounds and get back to you.

    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Ah got it - it is the separator: '' that is doing it! If you remove that the type is preserved but you get it in an array:

    {
        "data": {
            "0": {
                "IsDB": [1],
                "IsWebsite": ["false"],
                "IsOtherThingy": [true]
            }
        },
        "action": "create"
    }
    

    which all makes sense - doing a join on an array will always result in a string.

    Can you have the server accept the arrays of data?

    Allan

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    @allan Thanks for looking into that. For now, I will just use dropdowns with a "yes" and "no" option backed by a true bool.

    I am using ASPNET Core 3.1 with the latest System.Text.Json library and I do not want to have to build/find, test and support custom JSON converters on my back-end. For tech debt reasons but also performance reasons. Plus, I want to try and get away from having my UI framework dictate the API design of my back-end since I have many more clients than just JS and DTE. While I do recognize that is unavoidable a lot of the time.

    So for those reasons, I have decided to lean into the out of the box functionality of the new JSON API in .NET Core and have my API be a little more strict in terms of not coercing strings to things like numbers and bools.

This discussion has been closed.