October: Saving a multiselect dropdown on backend form

Created on 4 Mar 2016  路  6Comments  路  Source: octobercms/october

I'm trying to render a multiselect dropdown on backend form by using field property :

    nearby_destinations:
        label: Nearby Destinations
        type: dropdown
        attributes:
            multiple: true

this is properly rendering as multiselect dropdown but while saving it only saves last value to database. Ive tried to make it jsonable but still not working.
Please help!

Question

Most helpful comment

There has been a new taglist form field that is essentially a multiple select dropdown.

All 6 comments

I asked this question on SO
http://stackoverflow.com/questions/35620124/october-cms-create-a-multi-select-form-field/35786243#35786243
It work but it is limited you can only use yaml options

Just use a type partial instead and render anything you like. Dropdown does not support multiple at this stage. I assume it doesn't work because the input name must be an array, eg: name=MyModel[nearby_destinations][]

Copy the content from modules\backend\widgets\form\partials\_field_dropdown.htm to get an idea of what to put in the partial.

yes @daftspunk , as I can see from chrome console is that it returns :

Destinations[nearby_destinations]:value1
Destinations[nearby_destinations]:value2

two times... thats why it doesn't work.
and finally it only saves last one.

So is there any way to turn it into array if its multiselect.?

I was trying partial but not aware of what to put in partial and also how to get all options rendered in it.
I'll try ur suggestion and reply..!
Thanks!!

<?php
    $fieldOptions = $field->options();
?>
<!-- Dropdown -->
<?php if ($this->previewMode): ?>
    <div class="form-control"><?= (isset($fieldOptions[$field->value])) ? e(trans($fieldOptions[$field->value])) : '' ?></div>
<?php else: ?>
    <select
        id="<?= $field->getId() ?>"
        name="<?= $field->getName() ?>[]"
        class="form-control custom-select"
        multiple
        <?= $field->getAttributes() ?>>
        <?php if ($field->placeholder): ?>
            <option value=""><?= e(trans($field->placeholder)) ?></option>
        <?php endif ?>
        <?php foreach ($fieldOptions as $value => $option): ?>
            <?php
                if (!is_array($option)) $option = [$option];
            ?>
            <option
                <?= $value == $field->value ? 'selected="selected"' : '' ?>
                <?php if (isset($option[1])): ?>data-<?=strpos($option[1],'.')?'image':'icon'?>="<?= $option[1] ?>"<?php endif ?>
                value="<?= $value ?>">
                    <?= e(trans($option[0])) ?>
            </option>
        <?php endforeach ?>
    </select>
<?php endif?>

Note name="<?= $field->getName() ?>[]" that should pass an array instead.

There has been a new taglist form field that is essentially a multiple select dropdown.

Thanks for this update. !

Was this page helpful?
0 / 5 - 0 ratings