4

我需要在十月 Cms 后端表单(fields.yaml)中创建一个多选表单字段。我应用了以下方法

select_field:
     标签:样本
     类型:下拉
     属性:{multiple:'multiple'}

该字段的选项将从模型中调用

一切正常,但是当我提交表单时,只有第一个选择的选项作为 JSON 数据插入到数据库表文本字段中。我希望存储每个选定的选项。我还在模型中将该字段声明为 Jsonable,即 protected $jsonable = ['field_name'];

注意:当我将类型用作复选框列表时,它按我的想法工作,但我不希望它是复选框列表。我是十月 cms 的新手,有什么简单的方法吗?

4

5 回答 5

1

您不能使用具有多个值的下拉字段,因为它是用来处理单个值的。作为复选框和收音机,这就是为什么有 checkboxList(我猜)。

但我找到了解决方案。在 fields.yaml 中使用部分字段类型而不是下拉列表

https://octobercms.com/docs/backend/forms#field-partial

使用以下内容创建一个部分(注意名称属性中的 [] 这就是它起作用的原因!)

但请注意,这只是一个技巧,您只能使用直接 yaml 选项分配 https://octobercms.com/docs/backend/forms#field-dropdown

<?php
    $fieldOptions = $field->options();
    //get the field value as an array
    $selectedValues = (array)$field->value;

?>
<!-- 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"
        <?= $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

                <?= in_array($value, $selectedValues)  ? '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?>

对于 yaml

```
select_field:
    label: Sample
    type: partial
    path:$/author/plugin/models/classfolder/_my_partial.htm
    attributes: {multiple:'multiple'}
    options:
        key:value
        key:value
```

如果您有能力触摸核心并添加

\modules\backend\widgets\form\partials 中的相同内容,名称为 _field_dropdownlist.htm

然后在 \modules\backend\widgets\form\Form.php 第 630 行更改:

$optionModelTypes = ['dropdown', 'radio', 'checkboxlist', 'balloon-selector'];

添加不带 _field 或 .htm 的部分名称 ex _field_dropdownlist.htm 成为 dropdowList

$optionModelTypes = ['dropdown', 'radio', 'checkboxlist', 'balloon-selector','dropdowlist'];

现在在您的 yaml 文件中,只需使用 type:dropdownList 即可。

于 2016-03-04T01:13:50.957 回答
0

要通过表单处理多对多关系,您可以使用多选输入或复选框。在 october cms 中,有几个关系表单小部件taglist 表单小部件来处理这种情况。
在 fields.yaml 中定义以下内容:

# relational form widget
categories: # relationship name  defined in the model
  label: 'Associated categories'
  span: full
  required: 1
  type: relation
  nameFrom: title # db column name to fetch to show the display value

#taglist form widget
  categories:
    label: 'Associated categories'
    span: full
    mode: relation
    required: 1
    type: taglist
    nameFrom: title
于 2019-08-24T07:53:26.883 回答
0

你可以使用 select2 jquery 插件

_widget.htm

<!-- for booking table -->
<select  class="form-control" id="sle1" name="Booking[business_car_id]" >

    <?php   foreach($businesscars as $key => $value): ?>
        <option class="form-control" value="<?php echo $key; ?>" >
             <?php
            echo $value; ?>  
        </option>

    <?php endforeach; ?>
</select>

并将 select2.js 和 select2.css 分别放在 assets/js 和 assets/css forlder 中

从这里下载选择 2 https://select2.org/getting-started/basic-usage

于 2018-12-14T08:15:10.543 回答
0

您可以使用“taglist”小部件来执行此操作;-)

于 2017-07-12T10:02:44.233 回答
0

daftspunk 还提出了一种解决方法:https ://github.com/octobercms/october/issues/1829#issuecomment-193018469

于 2016-03-18T06:05:01.217 回答