3

I have been trying several configurations for trying to update a user account checkbox to uncheck it, with no success. I have been able to check it without issue.

updateCharType = function (token) {

    var obj = {
        "uid": "1",
        "name": "admin",
        "field_character_type": {"und": [{"value": 0}]}
    };
    return $.ajax({
        url: 'www.example.com/endpoint/1.json',
        crossDomain: true,
        type: 'PUT',
        data: obj,
        dataType: 'json',
        headers: {
            'X-CSRF-Token': token
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log('user edit - failed to edit ' + textStatus);
            console.log(errorThrown);
        },
        success: function (data) {
            alert(success!);
        }
    });
}

Now, it always gets to the success portion of the code, and it seems like it is working correctly.

However the checkbox never gets unchecked. I've read a bunch of documentation, and tried a million different iterations of how the JSON should be formed - putting null, removing the [], removing the und - for some reason I am having absolutely zero luck updating this checkbox!

How do I update a checkbox on a user account with services?

EDIT:

I WAS able to get it working with a list of values for a different field:

 var fontSize = jQuery('#font-size').val();

    var obj = {
        "uid": "1",
        "name": "admin",
        "field_font_size": {"und": fontSize}
    };
    return $.ajax({
        url: 'http://example.com' + "/users/user/1.json",
        crossDomain: true,
        type: 'PUT',
        data: obj,
        dataType: 'application/json',
        headers: {
            'X-CSRF-Token': token
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log('user edit - failed to edit ' + textStatus);
            console.log(errorThrown);
        },
        success: function (data) {
            alert("Success for user save!:) ");
        }
    });
};

Differences:

How to be a list of values, and I needed to set it to application/json

Boolean field still not working under any variations.

Also, whenever I try to uncheck the checkbox I get the following error:

Notice: Array to string conversion in DatabaseStatementBase->execute() (line 2227 of /Users/username/Desktop/Projects/example.com/includes/database/database.inc).
5
  • What widget are you using for the field? Commented Jul 30, 2018 at 22:23
  • The single checkbox widget
    – Jack Ryan
    Commented Jul 31, 2018 at 17:21
  • Is it a required field? Commented Jul 31, 2018 at 18:37
  • No, should it be?
    – Jack Ryan
    Commented Jul 31, 2018 at 18:37
  • Nope, optional is OK (especially if we want it unchecked). Commented Jul 31, 2018 at 18:38

2 Answers 2

2

To un-check a check box with Services in Drupal 7 (aka setting false on a boolean field), try this:

"field_character_type": {"und": null}

I've just verified this works on my localhost (for both users and nodes), here's the payload for the PUT:

{
  uid: 1,
  name: "admin",
  field_character_type: { und: null }
}

And here's the path:

api/user/1.json

Make sure you include the [my-endpoint-path]/user/[uid].json for the call.

You only need to cast the data payload into a string if you're using vanilla .js and an xhr object, otherwise jQuery's ajax does it automatically for you.

4
  • Nope, doesn't seem to work. And when I get the data returned on the .done method (I haven't included that, but can if you'd like), it shows the value for the und key being "" - empty string, versus null. The field is set to boolean and single on/off checkbox, if that helps
    – Jack Ryan
    Commented Jul 31, 2018 at 14:56
  • I am getting a lot of these warning messages whenever I try to update the checkbox with the above code: Notice: Array to string conversion in DatabaseStatementBase->execute() (line 2227 of /Users/username/Desktop/Projects/example.com/includes/database/database.inc). - might I need to cast my JSON into a string?
    – Jack Ryan
    Commented Jul 31, 2018 at 16:39
  • I've updated my answer with a bit more info and a working example, good luck and happy coding! Commented Jul 31, 2018 at 18:44
  • Can you put the exact JS you're using for submission? Like including the submit and everything? I was unable to get this to work at all, despite trying a ridiculous number of variations. I put a 200 point bounty that is about to expire and I am happy to award it
    – Jack Ryan
    Commented Aug 10, 2018 at 22:13
0

Ultimately, the value coming through was always empty string, never null, even when I directly set it null.

The solution to my problem was this:

function MY_MODULE_services_request_preprocess_alter($controller, &$args, $options) {
  if(!is_null($args[1]['field_character_type']) && $args[1]['field_character_type']['und'] == "") {
      $args[1]['field_character_type']['und'] = null;
  }
}

That was with sending this JSON object to the server:

    var obj = {
        field_character_type: { und: null }
    };

And the services call:

    return $.ajax({
        url: 'http://example.com' + "/users/user/1.json",
        crossDomain: true,
        type: 'PUT',
        data: obj,
        dataType: 'json',
        headers: {
            'X-CSRF-Token': token
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log('user edit - failed to edit ' + textStatus);
            console.log(errorThrown);
        },
        success: function (data) {
            //alert("Success for user save!:) ");
        }
    });
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.