feat(admin): edit category content in the admin form behind the HTML gate
- add a content textarea (size huge) after description, with en and pl phrases - add content to Category.Fillable - export classes.CheckHTML and run it from FormBeforeCreate and FormBeforeUpdate: unsafe category content answers 422 on content, blank content is stored as NULL
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"git.golem15.com/golem15/sm-journal-plugin/classes"
|
||||
"git.golem15.com/golem15/sm-journal-plugin/models"
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/cabana"
|
||||
@@ -40,9 +45,46 @@ func (categoriesAdminController) AdminFieldRelations() []cabana.FieldRelationCon
|
||||
}}
|
||||
}
|
||||
|
||||
func (categoriesAdminController) FormBeforeCreate(ctx context.Context, model any) error {
|
||||
cat, ok := model.(*models.Category)
|
||||
if !ok || cat == nil {
|
||||
return errors.New("categories admin: form model is not a category")
|
||||
}
|
||||
return prepareCategoryWrite(cat)
|
||||
}
|
||||
|
||||
func (categoriesAdminController) FormBeforeUpdate(ctx context.Context, model any) error {
|
||||
cat, ok := model.(*models.Category)
|
||||
if !ok || cat == nil {
|
||||
return errors.New("categories admin: form model is not a category")
|
||||
}
|
||||
return prepareCategoryWrite(cat)
|
||||
}
|
||||
|
||||
// prepareCategoryWrite gates the category HTML body (D-21): blank content is
|
||||
// stored as NULL, and content that fails classes.CheckHTML is a 422 on
|
||||
// content. Any other content is stored exactly as submitted.
|
||||
func prepareCategoryWrite(cat *models.Category) error {
|
||||
if cat.Content == nil {
|
||||
return nil
|
||||
}
|
||||
if strings.TrimSpace(*cat.Content) == "" {
|
||||
cat.Content = nil
|
||||
return nil
|
||||
}
|
||||
if err := classes.CheckHTML(*cat.Content); err != nil {
|
||||
return &cabana.ValidationError{Details: map[string]any{
|
||||
"content": []string{err.Error()},
|
||||
}}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
_ pact.AdminController = categoriesAdminController{}
|
||||
_ pact.AdminPermissioned = categoriesAdminController{}
|
||||
_ pact.AdminRecordSource = categoriesAdminController{}
|
||||
_ pact.FormBeforeCreate = categoriesAdminController{}
|
||||
_ pact.FormBeforeUpdate = categoriesAdminController{}
|
||||
_ cabana.FieldRelationProvider = categoriesAdminController{}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user