Skip to content

Commit

Permalink
adding interface for Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni authored and markbates committed Feb 6, 2020
1 parent 5bf85b5 commit 798d64d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion form/bootstrap/form_for.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/gobuffalo/tags/v3"
"github.com/gobuffalo/tags/v3/form"
"github.com/gobuffalo/validate/validators"
"github.com/gobuffalo/validate/v3/validators"
)

//FormFor is the FormFor version for bootstrap
Expand Down
18 changes: 17 additions & 1 deletion form/bootstrap/form_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/gobuffalo/tags/v3"
"github.com/gobuffalo/validate"
"github.com/gobuffalo/validate/v3"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -159,6 +159,22 @@ func Test_CheckBox(t *testing.T) {
r.Equal(`<div class="form-group"><label><input class="" id="-Name" name="Name" type="checkbox" value="true" /> Custom</label></div>`, l.String())
}

type CustomError struct{}

func (ce CustomError) Get(key string) []string {
return []string{"My Custom Error"}
}

func Test_InputError_CustomError(t *testing.T) {
r := require.New(t)

errors := CustomError{}

f := NewFormFor(struct{ Name string }{}, tags.Options{"errors": errors})
l := f.InputTag("Name", tags.Options{"label": "Custom"})
r.Equal(`<div class="form-group has-error"><label>Custom</label><input class=" form-control is-invalid" id="-Name" name="Name" type="text" value="" /><div class="invalid-feedback help-block">My Custom Error</div></div>`, l.String())
}

func Test_InputError(t *testing.T) {
r := require.New(t)

Expand Down
5 changes: 5 additions & 0 deletions form/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package form

type Errors interface {
Get(key string) []string
}
31 changes: 17 additions & 14 deletions form/form_for.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/gobuffalo/flect"
"github.com/gobuffalo/tags/v3"
"github.com/gobuffalo/validate"
"github.com/gobuffalo/validate/v3"
"github.com/gofrs/uuid"
)

Expand All @@ -24,7 +24,7 @@ type FormFor struct {
name string
dashedName string
reflection reflect.Value
Errors *validate.Errors
Errors Errors
}

// NewFormFor creates a new Formfor with passed options, it also creates the id of the form from the struct name and adds errors if present.
Expand Down Expand Up @@ -53,20 +53,23 @@ func NewFormFor(model interface{}, opts tags.Options) *FormFor {
}
}

func loadErrors(opts tags.Options) *validate.Errors {
func loadErrors(opts tags.Options) Errors {
errors := validate.NewErrors()
if opts["errors"] != nil {
switch t := opts["errors"].(type) {
default:
fmt.Printf("Unexpected errors type %T, please\n", t) // %T prints whatever type t has
case map[string][]string:
errors = &validate.Errors{
Errors: opts["errors"].(map[string][]string),
Lock: new(sync.RWMutex),
}
case *validate.Errors:
errors = opts["errors"].(*validate.Errors)

if opts["errors"] == nil {
return errors
}

switch t := opts["errors"].(type) {
default:
fmt.Printf("Unexpected errors type %T, please\n", t) // %T prints whatever type t has
case map[string][]string:
errors = &validate.Errors{
Errors: opts["errors"].(map[string][]string),
Lock: new(sync.RWMutex),
}
case Errors:
return opts["errors"].(Errors)
}

return errors
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/fatih/structs v1.1.0
github.com/gobuffalo/flect v0.2.0
github.com/gobuffalo/validate v2.0.4+incompatible
github.com/gobuffalo/validate/v3 v3.0.0
github.com/gofrs/uuid v3.2.0+incompatible
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.9.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/gobuffalo/flect v0.2.0 h1:EWCvMGGxOjsgwlWaP+f4+Hh6yrrte7JeFL2S6b+0hdM
github.com/gobuffalo/flect v0.2.0/go.mod h1:W3K3X9ksuZfir8f/LrfVtWmCDQFfayuylOJ7sz/Fj80=
github.com/gobuffalo/validate v2.0.4+incompatible h1:ZTxozrIw8qQ5nfhShmc4izjYPTsPhfdXTdhXOd5OS9o=
github.com/gobuffalo/validate v2.0.4+incompatible/go.mod h1:N+EtDe0J8252BgfzQUChBgfd6L93m9weay53EWFVsMM=
github.com/gobuffalo/validate/v3 v3.0.0 h1:dF7Bg8NMF9Zv8bZvUMXYJXxZdj+eSZ8z/lGM7/jVFUE=
github.com/gobuffalo/validate/v3 v3.0.0/go.mod h1:HFpjq+AIiA2RHoQnQVTFKF/ZpUPXwyw82LgyDPxQ9r0=
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
Expand Down

0 comments on commit 798d64d

Please sign in to comment.