Skip to content

Commit

Permalink
Don't register duplicated callbacks for validations
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Dec 28, 2017
1 parent 404d5a9 commit f364bca
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func formattedError(err govalidator.Error, resource interface{}) error {
// RegisterCallbacks register callback into GORM DB
func RegisterCallbacks(db *gorm.DB) {
callback := db.Callback()
callback.Create().Before("gorm:before_create").Register("validations:validate", validate)
callback.Update().Before("gorm:before_update").Register("validations:validate", validate)
if callback.Create().Get("validations:validate") == nil {
callback.Create().Before("gorm:before_create").Register("validations:validate", validate)
}
if callback.Update().Get("validations:validate") == nil {
callback.Update().Before("gorm:before_update").Register("validations:validate", validate)
}
}

0 comments on commit f364bca

Please sign in to comment.