Sì, mi riferisco al limite massimo di oggetti nell’array.
Attualmente non è supportato ufficialmente, quindi ho esteso questa funzionalità nel mio componente tema privato personalizzato. La configurazione è la seguente (solo a titolo di riferimento)
Configurazione del limite massimo per gli array:
themes/quectel-custom-homepage/settings.yml
banner_images:
type: objects
default: []
schema:
name: "Immagine carosello"
max: 9 // Configurazione estesa aggiunta: limita il numero massimo di elementi nell'array
identifier: image_url
properties:
image_url:
type: upload
required: true
link_url:
type: string
min: 0
max: 1023
Codice specifico dell’estensione:
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(() => {
// Estende il componente SchemaSettingNewEditor usando classPrepend per supportare il limite max per il tipo objects
// Carica questo componente solo nel contesto amministratore
let SchemaSettingNewEditor;
try {
SchemaSettingNewEditor =
require("discourse/admin/components/schema-setting/editor").default;
} catch {
// Gli utenti non amministratori non possono caricare questo componente, salta l'estensione
return;
}
classPrepend(SchemaSettingNewEditor, (Superclass) => {
return class extends Superclass {
@action
addItem() {
// Controlla se è stato raggiunto il limite 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];
// Controlla se è stato raggiunto il limite 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);
}
};
});
});
Configurazione delle traduzioni del tema:
themes/quectel-custom-homepage/locales/zh_CN.yml
zh_CN:
theme_settings:
errors:
objects_value_not_valid_max: "Il numero non può superare %{count}"
Effetto concreto:
- Messaggio di errore
- Impossibile aggiungere altri elementi
