是的,我是指限制 objects 数组的最大个数。
目前官方不支持,所以我在自定义私有主题组件中扩展了对应功能,配置如下(仅供参考)
最大数组个数限制配置:
themes/quectel-custom-homepage/settings.yml
banner_images:
type: objects
default: []
schema:
name: "轮播图"
max: 9 // 新增的扩展配置,限制数组的最大个数
identifier: image_url
properties:
image_url:
type: upload
required: true
link_url:
type: string
min: 0
max: 1023
扩展的具体代码:
themes/quectel-custom-homepage/javascripts/discourse/api-initializers/quectel-custom-homepage.gjs
import { action } from "@ember/object";
import { apiInitializer } from "discourse/lib/api";
import classPrepend from "discourse/lib/class-prepend";
import { i18n } from "discourse-i18n";
export default apiInitializer(() => {
// 使用 classPrepend 扩展 SchemaSettingNewEditor 组件以支持 objects 类型的 max 限制
// 仅在管理员上下文中加载该组件
let SchemaSettingNewEditor;
try {
SchemaSettingNewEditor =
require("discourse/admin/components/schema-setting/editor").default;
} catch {
// 非管理员用户无法加载该组件,跳过扩展
return;
}
classPrepend(SchemaSettingNewEditor, (Superclass) => {
return class extends Superclass {
@action
addItem() {
// 检查是否达到 max 限制
const maxLimit = this.args.setting.max || this.activeSchema?.max;
if (maxLimit && this.activeData.length >= maxLimit) {
this.validationErrorMessage = i18n(
themePrefix("theme_settings.errors.objects_value_not_valid_max"),
{
count: maxLimit,
}
);
return;
}
return super.addItem(...arguments);
}
@action
addChildItem(propertyName, parentNodeIndex) {
const propertySchema = this.activeSchema.properties[propertyName];
// 检查是否达到 max 限制
if (
propertySchema?.max &&
this.activeData[parentNodeIndex][propertyName].length >=
propertySchema.max
) {
this.validationErrorMessage = i18n(
themePrefix("theme_settings.errors.objects_value_not_valid_max"),
{
count: propertySchema.max,
}
);
return;
}
return super.addChildItem(...arguments);
}
};
});
});
主题翻译配置:
themes/quectel-custom-homepage/locales/zh_CN.yml
zh_CN:
theme_settings:
errors:
objects_value_not_valid_max: "数量不能超过 %{count} 个"
具体效果:
- 错误提示
- 无法添加更多
