How to save multiple values with plugin select2 as one field in database

How to save multiple values with plugin select2 as one field in database

Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0
edited November 2017 in Editor

I've got the following js code to select multiple options.

{
                "label": "Bodemvocht:",
                "name": "bodemvocht",
                "type": "select2",
                "options": [
                     { "label": "", "value": "" },
                     { "label": "Droog", "value": "Droog" },
                     { "label": "Vochtig", "value": "Vochtig" },
                     { "label": "Nat", "value": "Nat" },
                     { "label": "Geen voorkeur", "value": "Geen voorkeur" }
                ],
                "opts": {
               "placeholder": "Selecteer vochtigheid",
               "multiple": "multiple",
               "separator": ";",
               "allowClear": true
                }

But when saving this I get an error (Array to string conversion) and only the text 'Array' is saved in the database. How to solve this properly?

Thanks for your help!

Answers

  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin

    I don't think that Select2 actually has a separator option does it? The Select2 docs suggest not. There is however tokenSeparators which I think will do what you are looking for.

    Alternatively, I have an updated version of the Select2 plug-in for Editor which does have its own separator option that I could send over? It hasn't been published yet.

    Regards,
    Allan

  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0
    edited November 2017

    Allan, thanks for your answer and sorry for the late reply! I expected (and didn't get) an email notification of your post. I got the separator option from https://editor.datatables.net/plug-ins/field-type/editor.select2. I tried tokenSeparators but it still wouldn't work unfortunately.
    I have been troubleshooting some time but it still isn't functioning. I'm certainly interested in the updated select2 plug-in (or some other solution).

  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin

    Sent you a PM with it. I'll be publishing it probably next week.

    Allan

  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0

    Hi Allan,
    Again sorry for the late reply. Based on your example script the problem was fixed simply by moving this --> separator: ";" <-- upwards from the 'opts' part to the 'main settings' part. Works perfect now. Thanks a lot!

    Koen

This discussion has been closed.