Skip to content

Commit

Permalink
Merge pull request #1 from karlhaas/fix-create-eager-validation
Browse files Browse the repository at this point in the history
fix: preserve eager information when validating models (gobuffalo#664)
  • Loading branch information
karlhaas committed Oct 2, 2021
2 parents 4c1da2d + ada61eb commit 3b594c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions executors.go
Expand Up @@ -97,6 +97,10 @@ func (c *Connection) Save(model interface{}, excludeColumns ...string) error {
// If model is a slice, each item of the slice is validated then created in the database.
func (c *Connection) ValidateAndCreate(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := NewModel(model, c.Context())

isEager := c.eager
hasEagerFields := c.eagerFields

if err := sm.beforeValidate(c); err != nil {
return nil, err
}
Expand Down Expand Up @@ -155,6 +159,8 @@ func (c *Connection) ValidateAndCreate(model interface{}, excludeColumns ...stri
}
}

c.eager = isEager
c.eagerFields = hasEagerFields
return verrs, c.Create(model, excludeColumns...)
}

Expand Down
4 changes: 4 additions & 0 deletions pop_test.go
Expand Up @@ -155,6 +155,10 @@ type Taxis []Taxi
// Validate gets run every time you call a "Validate*" (ValidateAndSave, ValidateAndCreate, ValidateAndUpdate) method.
// This method is not required and may be deleted.
func (b *Book) Validate(tx *Connection) (*validate.Errors, error) {
// Execute another query to test if Validate causes eager creation to fail
if err := tx.All(&Taxis{}); err != nil {
return nil, err
}
return validate.Validate(
&validators.StringIsPresent{Field: b.Description, Name: "Description"},
), nil
Expand Down

0 comments on commit 3b594c1

Please sign in to comment.