10月CMS创建一个多选表单字段 [英] October CMS create a multi select Form field
问题描述
我需要在10月份的Cms后端表单(fields.yaml)中创建一个多选表单字段。我申请了以下方法:
select_field:
label:Sample
类型:dropdown
属性:{multiple:'multiple'}
这个字段的选项将从模型中调用
一切正常,但是当我提交表单时,只有第一个选中的选项被插入为数据库表文本字段中的JSON数据。我希望每个选定的选项都被存储。我也已经声明这个字段在模型中是Jsonable,例如,protected $ jsonable = ['field_name'];
注意:当我使用类型 checkboxlist 时,它按照我的想法工作,但我不想要成为checkboxlist。我是新来的十月cms是否有任何简单的方法..
你不能使用具有多个值的下拉字段因为它是为了处理单一的价值。作为复选框和广播这就是为什么有checkboxList(我猜)。
但我找到了一个解决方案。
在fields.yaml中使用partial作为字段类型,而不是下拉菜单
https://octobercms.com/docs/backend/forms#field-partial
创建一个部分(注意名称属性中的[]这就是它工作的原因!)
但请注意,这只是一个窍门,您只能使用直接yaml选项分配 https://octobercms.com/docs/backend/
<?php
$ fieldOptions = $ field-> options ();
//将字段值作为数组获取
$ selectedValues =(array)$ field-> value;
?>
<! - 下拉菜单 - >
<?php if($ this-> previewMode):?>
< div class =form-control><?=(isset($ fieldOptions [$ field-> value]))? e(trans($ fieldOptions [$ field-> value])):''?>< / div>
<?php else:?>
id =<?= $ field-> getId()?>
name =<?= $ field-> getName()?> []
class =form-control custom-select
<?= $ field- > getAttributes()?>>
<?php if($ field->占位符):?>
< 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:示例
类型:partial
路径:$ / author / plugin / models / classfolder / _my_partial.htm
属性:{multiple:'multiple'}
选项:
键:值
键:值
```
如果您有能力触摸内核并添加内容,则可以更好地构建窗口小部件或进行拉取请求
<\\ p> \模块\后端\ widgets \ form \部分中的相同内容
带名称_field_dropdownlist.htm
然后在\modules\backend\widgets\form\Form.php第630行
更改:
$ optionModelTypes = ['dropdown','radio','checkboxlist','balloon-selector'];
将您的部分名称添加为_field或.htm前_field_dropdownlist.htm成为dropdowList
$ optionModelTypes = ['dropdown','radio','checkboxlist','balloon-selector','dropdowlist'];
现在在您的yaml文件中使用类型:dropdownList,它将起作用。
I need to create a multi select form field in October Cms backend form (fields.yaml). I applied the following method
select_field:
label: Sample
type: dropdown
attributes: {multiple:'multiple'}
Options to this field will be invoked from the model
Everything is working good but when i submit the form only the first selected option is being inserted as JSON data in the database table text field. I want every selected option to be stored. I have also declared the field as Jsonable in model ie., protected $jsonable = ['field_name'];
Note : When I use type as checkboxlist it is working as I thought but I don't want it to be checkboxlist. I'm new to October cms is there any easy way..
you can't use a dropdown field with mutiple value because it was made to handle single value. as the checkbox and radio this is why there are checkboxList(i guess).
But i found a solution. Use a partial as field type in fields.yaml instead of dropdown
https://octobercms.com/docs/backend/forms#field-partial
Create a partial with the content below (notice the [] in name attribute this is why it work !)
But be aware that this is just a trick and you can only use direct yaml options assignment 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?>
And for the yaml
```
select_field:
label: Sample
type: partial
path:$/author/plugin/models/classfolder/_my_partial.htm
attributes: {multiple:'multiple'}
options:
key:value
key:value
```
A better approche would be may be to build a widget or make a pull request if you have the ability you can touch the core and add
the same content in \modules\backend\widgets\form\partials with name _field_dropdownlist.htm
then in \modules\backend\widgets\form\Form.php line 630 change :
$optionModelTypes = ['dropdown', 'radio', 'checkboxlist', 'balloon-selector'];
to add your partial name without _field or .htm ex _field_dropdownlist.htm become dropdowList
$optionModelTypes = ['dropdown', 'radio', 'checkboxlist', 'balloon-selector','dropdowlist'];
now in your yaml file juste use type:dropdownList and it will work.
这篇关于10月CMS创建一个多选表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!