8000 Section validator only show GlobalError by ZLValien · Pull Request #363 · qor5/admin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Section validator only show GlobalError #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/docsrc/examples/examples_presets/detailing_inline_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@
return
}

func PresetsDetailInlineEditValidate(b *presets.Builder, db *gorm.DB) (
cust *presets.ModelBuilder,
cl *presets.ListingBuilder,
ce *presets.EditingBuilder,
dp *presets.DetailingBuilder,
) {
err := db.AutoMigrate(&Customer{}, &CreditCard{}, &Note{})
if err != nil {
panic(err)

Check warning on line 116 in docs/docsrc/examples/examples_presets/detailing_inline_edit.go

View check run for this annotation

Codecov / codecov/patch

docs/docsrc/examples/examples_presets/detailing_inline_edit.go#L116

Added line #L116 was not covered by tests
}
b.DataOperator(gorm2op.DataOperator(db))

cust = b.Model(&Customer{})
// This should inspect Notes attributes, When it is a list, It should show a standard table in detail page
dp = cust.Detailing("name_section").Drawer(true)
dp.Section("name_section").Label("name must not be empty").Editing("Name").Viewing("Name").Validator(func(obj interface{}, ctx *web.EventContext) (err web.ValidationErrors) {
customer := obj.(*Customer)
if customer.Name == "" {
err.GlobalError("customer name must not be empty")
}
return
})

return
}

func PresetsDetailNestedMany(b *presets.Builder, db *gorm.DB) (
mb *presets.ModelBuilder,
cl *presets.ListingBuilder,
Expand Down
26 changes: 26 additions & 0 deletions docs/docsrc/examples/examples_presets/detailing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,29 @@ func TestPresetsDetailActionsComponent(t *testing.T) {
})
}
}

func TestPresetsDetailSectionValidate(t *testing.T) {
pb := presets.New().DataOperator(gorm2op.DataOperator(TestDB))
PresetsDetailInlineEditValidate(pb, TestDB)

cases := []multipartestutils.TestCase{
{
Name: "page detail show",
Debug: true,
ReqFunc: func() *http.Request {
detailData.TruncatePut(SqlDB)
return multipartestutils.NewMultipartBuilder().
PageURL("/customers?__execute_event__=presets_Detailing_Field_Save&detailField=name_section&id=12").
AddField("name_section.Name", "").
BuildEventFuncRequest()
},
ExpectRunScriptContainsInOrder: []string{"message: \"customer name must not be empty\""},
},
}

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
multipartestutils.RunCase(t, c, pb)
})
}
}
1 change: 1 addition & 0 deletions docs/docsrc/examples/examples_presets/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
addExample(mux, db, PresetsDetailInlineEditInspectTables)
addExample(mux, db, PresetsDetailNestedMany)
addExample(mux, db, PresetsDetailInlineEditFieldSections)
addExample(mux, db, PresetsDetailInlineEditValidate)

Check warning on line 43 in docs/docsrc/examples/examples_presets/mux.go

View check run for this annotation

Codecov / codecov/patch

docs/docsrc/examples/examples_presets/mux.go#L43

Added line #L43 was not covered by tests
addExample(mux, db, PresetsDetailSimple)
return
}
Expand Down
4 changes: 2 additions & 2 deletions presets/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ func (b *SectionBuilder) DefaultSaveFunc(obj interface{}, id string, ctx *web.Ev
}

if b.validator != nil {
if vErr := b.validator(obj, ctx); vErr.HaveErrors() {
return errors.New(vErr.Error())
if vErr := b.validator(obj, ctx); vErr.GetGlobalError() != "" {
return errors.New(vErr.GetGlobalError())
}
}
err = b.father.mb.editing.Saver(obj, id, ctx)
Expand Down
Loading
0