0

I've been developing this phonegap app and I'm stuck with a few things and one of them is multiple checkboxes - I hope you can help.

I've got the below code

HTML

<input type="date" id="date" />
<input type="hidden" id="hidden">
<fieldset id="oyuncuKategori">
    <legend>Kategori</legend>
    <div class="roundedOne">
        <input type="checkbox" id="oyuncu" name="kategori" value="oyuncu" class="squareTwo" />
        <label for="oyuncu">Oyuncu</label>
    </div>
    <div class="roundedOne">
        <input type="checkbox" id="yardimci_oyuncu" name="kategori" value="yardimci_oyuncu" class="squareTwo" />
        <label for="yardimci_oyuncu">Yardımcı oyuncu</label>
    </div>
    <div class="roundedOne">
        <input type="checkbox" id="cocuk_oyuncu" name="kategori" value="cocuk_oyuncu" class="squareTwo" />
        <label for="cocuk_oyuncu">Çocuk oyuncu</label>
    </div>
    <div class="roundedOne">
        <input type="checkbox" id="stand_hostesi" name="kategori" value="stand_hostesi" class="squareTwo" />
        <label for="stand_hostesi">Stand hostesi</label>
    </div>
</fieldset>

JavaScript

$("input[type='date']").on("change", function () {
    // var checkbox = $("#oyuncuKategori").serialize();
    var checkboxes = '';
    $('input[name=kategori]:checked').each(function(i, e) {
        checkboxes += '&node[field_kategorisi][und][value]='+$(this).val();
    });
    console.log(checkboxes);
    monthNames = [
        "Oca", "Şub", "Mar",
        "Nis", "May", "Haz", "Tem",
        "Ağu", "Eyl", "Eki",
        "Kas", "Ara"];
    date = new Date($("input").val());
    day = date.getDate();
    monthIndex = date.getMonth();
    year = date.getFullYear();
    finalDate = day + ' ' + monthNames[monthIndex] + ' ' + year;
    $.ajax({
        url: 'http://www.rejicast.com/services/node.json',
        type: 'post',
        dataType: 'json',
        data: 'node[type]=oyuncu&node[title]=testnomikos456&node[language]=und'+ checkboxes +'&node[field_dogum_tarihi][und][0][value][date]=' + finalDate +',
        success: function (data) {
            console.log(JSON.stringify(data));
        },
        error: function (xhr, status, message) {
            console.log(xhr);
            console.log(status);
            console.log(message);
        }
    });
});

The thing is, this code works for creating a node but and inserts values for field_kategorisi overwriting other values, thus always the last checked box is there, while trying with [und]['+i+'][value] gives me 406: An illegal choice has been detected. Please contact the site administrator. and something like Illegal choice Array in field_kategorisi (I don't remember the English wording) on the server side. I'm really stumped. Can you give me any ideas?

UPDATE: When I make an ajax call, my POST looks like this.

http://www.rejicast.com/services/node.json?node[type]=oyuncu&node[field_kategorisi][und][0][value]=stand_hostesi&node[field_kategorisi][und][1][value]=yesilcam

  • phonegap question in drupal? (though it is actually JS issue) – Sadi Oct 17 '15 at 6:41
  • I asked here because the server is Drupal. – Kartagis Oct 17 '15 at 6:42
  • sorry; I understand that after re-read the error message and the intended values – Sadi Oct 17 '15 at 6:50
  • hmm, also, come to think of it: how come you are sending data as as if it were application/x-www-form-urlencoded but you set the dataType to json? Does $.ajax convert it automatically? – wizonesolutions Oct 17 '15 at 17:38
  • I have enabled both json and application/x-www-form-urlencoded for my endpoint. As for if $.ajax converts it automatically, I don't know. – Kartagis Oct 17 '15 at 19:06
0

Edit: Just looked at the code more closely, and saw this was the Services module.

From https://www.drupal.org/node/1971484#comment-7318114:

For those of you who will probably have the same problem, here is the correct way to handle LIST type fields (in ex. below field_mosaic_bu):

{
  "type":"TEST",
  "title":"TITLE",
  "uid":"1",
  "field_mosaic_bu":{ "und": ["bu name"] },
  "field_mosaic_var1":{ "und": [{ "value":"test1" }] },
  "field_mosaic_var2":{ "und": [{ "value":"test2" }] },
  "field_mosaic_var3":{ "und": [{ "value":"test3" }] },
  "field_mosaic_image":{ "und": [{ "fid":"4" }] }  
}

Hope this helps someone. Took me a lot of time to figure this out.


When you get that message from Drupal, it generally means that you have tried to set a form value to a non-allowed value from the perspective of the field configuration. So, for example, field_kategorisi might be a Term Reference field. If you try to set it to a tid that isn't allowed according to the Drupal field configuration, that happens.

If it's a simple checkbox field, then adding the value you want to set to the Allowed Values in the field configuration should solve the issue.

If this doesn't help, please provide more information about the Drupal-side field configuration and whether you are using the form Drupal generates or not. It embeds various things like the form ID, form build ID, and so on into its form markup, so if problems persist, it could be related to it not receiving something it's expecting.

  • All the checkboxes' values are in the allowed values list. It even works if I omit ['+i+'], but overwriting all previous values. Please see dl.dropboxusercontent.com/u/11918318/… – Kartagis Oct 17 '15 at 8:53
  • Updated my answer when I saw this was talking to the Services module. Hope that helps. You can try searching the issue queue further searching for "illegal choice" if not: drupal.org/project/issues/… – wizonesolutions Oct 17 '15 at 9:06
  • Yea, that approach works for select fields, but not for checkboxes; because if I omit [0] it works, but also overwrites the value from the previous checkbox. – Kartagis Oct 17 '15 at 9:21
  • Can you add the actual POST JSON you are sending to the endpoint to your question? Should be able to find this in the Network tab. Generally, checkboxes expressed in PHP code are like this: array( 'value1' => TRUE, 'value3' => TRUE,) Empty values are not set or set to 0/FALSE – wizonesolutions Oct 17 '15 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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