Creating a custom field type plug-in for multiselect

Creating a custom field type plug-in for multiselect

mbrennandmbrennand Posts: 34Questions: 4Answers: 0
edited October 2013 in Editor
I'm struggling to come to terms with whats needed for a multiselect as a field type.
At the moment I have a field type select with the ipopts set to:

[code]"ipOpts":publicaions_loader() //function is called and select field is filled from get_publications.php[/code]

[code]//GET PUBLICATIONS
var publicaions = new Array({"label" : "a", "value" : "a"});
function publicaions_loader(){
publicaions.splice(0,1);
$.ajax({
url: 'db/get_publications.php',
async: false,
dataType: 'json',
success: function (json) {
for(var a=0;a

Replies

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    I'm going to guess that the value returned from the multiselect plug-in (is it a jQuery plug-in?) is an array. You need to either update what the server is expecting for that field to be an array (since it appears to be expecting a string) or modify the data being sent to the server to be a string (concatenate the values of the array for example).

    Allan
  • mbrennandmbrennand Posts: 34Questions: 4Answers: 0
    edited October 2013
    Could i do something like this, and then try push it back to the JSON:

    [code]"events": {
    "onPreSubmit": function (json, data) {
    if($.isArray(data.pubs_sent_to)) data.pubs_sent_to = data.pubs_sent_to.join(', ');
    }
    }
    [/code]

    But I get this error

    TypeError: data is undefined
    ...if($.isArray(data.pubs_sent_to)) data.pubs_sent_to = data.pubs_sent_to.join(', '
  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    I think you want `data.data.pubs_sent_to` there.

    Allan
  • mbrennandmbrennand Posts: 34Questions: 4Answers: 0
    edited October 2013
    Now im getting this:

    ReferenceError: data is not defined

    How would I insert it back into the JSON?
    Would doing the following replace the value of data.data.pubs_sent_to in the JSON? [code]data.data.pubs_sent_to = data.data.pubs_sent_to.join(', '); [/code]

    CHANGED IT TO THIS (now no type or reference error):
    [code] editor.on( 'onPreSubmit', function (json, data) {
    console.log(data.pubs_sent_to);
    if($.isArray(data.pubs_sent_to)) data.pubs_sent_to = data.pubs_sent_to.join(', ');
    console.log(data.pubs_sent_to);
    });
    [/code]

    [code]console.log(data.pubs_sent_to);[/code] returns undefined

    AND STILL GETTING THIS (Do I need to push it back to the JSON, if so how?):

    [code]

    Notice: Array to string conversion in /sysadmin/db/press_release/lib/Database/Driver/Mysql/Query.php on line 78

    {"id":"row_14","error":"","fieldErrors":[],"data":[],"row":{"DT_RowId":"row_14","id":"14","acyear":"2014\/2015","pubs_sent_to":"Array","name":"ghj","contact":"ghj","published":"fff","date":"ghj"}}[/code]
  • mbrennandmbrennand Posts: 34Questions: 4Answers: 0
    Solved....

    [code]
    "onPreSubmit": function (o) {
    if(o.action=="create"||o.action=="edit"){
    o.data.pubs_sent_to = o.data.pubs_sent_to.join(',');
    o.data.published = o.data.published.join(',');
    }
    }
    [/code]

    & for the form....
    [code]
    $('select', editor.node( 'pubs_sent_to' )).attr('multiple','multiple');
    $('select', editor.node( 'published' )).attr('multiple','multiple');
    [/code]
  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    Urgh - sorry. I clean missed that onPreSubmit can't have access to the JSON, so the prototype header using in the earlier posts ( `(json, data)` ) is obviously not right.

    Excellent to hear that you've got it working - thanks for posting your solution!

    Allan
This discussion has been closed.