feat(cabana): accept preset on mltext fields

- compilePresetKey admits a text or mltext target
- checkPresets admits a text or mltext source
- TestPresetML pins every text/mltext pairing and the refusals
This commit is contained in:
Jakub Zych
2026-10-06 21:09:48 +02:00
parent 7e2b1a909b
commit 7afad732c7
4 changed files with 193 additions and 13 deletions

View File

@@ -638,17 +638,18 @@ func compileWidgetKeys(typ string, values map[string]ast.Node, field *FormField)
}
// compilePresetKey decodes `preset` (D-27): the name of the field this text
// field follows while the administrator has not edited it, on create only. It
// is a string (the source field; type slug) or a mapping with the keys field
// and type, where type is slug or exact. The source is checked against the
// form in checkPresets.
// or mltext field follows while the administrator has not edited it, on
// create only. It is a string (the source field; type slug) or a mapping with
// the keys field and type, where type is slug or exact. An mltext pair
// follows per locale in the admin SPA. The source is checked against the form
// in checkPresets.
func compilePresetKey(typ string, values map[string]ast.Node, field *FormField) error {
node, ok := values["preset"]
if !ok {
return nil
}
if typ != "text" {
return fmt.Errorf("preset is only valid on type: text")
if typ != "text" && typ != "mltext" {
return fmt.Errorf("preset is only valid on type: text or mltext")
}
preset := FieldPreset{Type: "slug"}
switch n := node.(type) {
@@ -687,7 +688,7 @@ func compilePresetKey(typ string, values map[string]ast.Node, field *FormField)
}
// checkPresets checks every preset of a form against its fields: the source
// must be another text field of the same form.
// must be another text or mltext field of the same form.
func checkPresets(fields []FormField) error {
types := make(map[string]string, len(fields))
for _, field := range fields {
@@ -705,8 +706,8 @@ func checkPresets(fields []FormField) error {
if !ok {
return fmt.Errorf("field %s: preset field %s is not a field of this form", field.Name, source)
}
if typ != "text" {
return fmt.Errorf("field %s: preset field %s must be a text field", field.Name, source)
if typ != "text" && typ != "mltext" {
return fmt.Errorf("field %s: preset field %s must be a text or mltext field", field.Name, source)
}
}
return nil