Skip to content

Commit

Permalink
Merge pull request #53 from alphonse927/patch-1
Browse files Browse the repository at this point in the history
update to match Bootstrap v4 form validation
  • Loading branch information
paganotoni authored Jan 22, 2018
2 parents c03d5c6 + 4a7c17f commit ad62447
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions form/bootstrap/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import (
"github.com/markbates/inflect"
)

func buildOptions(opts tags.Options) {
func buildOptions(opts tags.Options, err bool) {
if opts["class"] == nil {
opts["class"] = ""
}
opts["class"] = strings.Join([]string{fmt.Sprint(opts["class"]), "form-control"}, " ")

if err {
opts["class"] = strings.Join([]string{fmt.Sprint(opts["class"]), "is-invalid"}, " ")
}

delete(opts, "hide_label")
}

Expand Down Expand Up @@ -49,19 +54,17 @@ func divWrapper(opts tags.Options, fn func(opts tags.Options) tags.Body) *tags.T
delete(opts, "label")
}

buildOptions(opts)
buildOptions(opts, hasErrors)

div.Append(fn(opts))

if hasErrors {
for _, err := range errors {
div.Append(tags.New("span", tags.Options{
"class": "help-block",
div.Append(tags.New("div", tags.Options{
"class": "invalid-feedback help-block",
"body": err,
}))
}

}

return div
}
8 changes: 4 additions & 4 deletions form/bootstrap/form_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func Test_InputError(t *testing.T) {

f := bootstrap.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" id="-Name" name="Name" type="text" value="" /><span class="help-block">Name shoud be AJ.</span></div>`, l.String())
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">Name shoud be AJ.</div></div>`, l.String())
}

func Test_InputError_Map(t *testing.T) {
Expand All @@ -120,7 +120,7 @@ func Test_InputError_Map(t *testing.T) {

f := bootstrap.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" id="-Name" name="Name" type="text" value="" /><span class="help-block">Name shoud be AJ.</span></div>`, l.String())
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">Name shoud be AJ.</div></div>`, l.String())
}

func Test_InputError_InvalidMap(t *testing.T) {
Expand All @@ -144,7 +144,7 @@ func Test_InputMultipleError(t *testing.T) {

f := bootstrap.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" id="-Name" name="Name" type="text" value="" /><span class="help-block">Name shoud be AJ.</span><span class="help-block">Name shoud start with A.</span></div>`, l.String())
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">Name shoud be AJ.</div><div class="invalid-feedback help-block">Name shoud start with A.</div></div>`, l.String())
}

func Test_CheckBoxError(t *testing.T) {
Expand All @@ -155,7 +155,7 @@ func Test_CheckBoxError(t *testing.T) {

f := bootstrap.NewFormFor(struct{ Name string }{}, tags.Options{"errors": errors})
l := f.CheckboxTag("Name", tags.Options{"label": "Custom"})
r.Equal(`<div class="form-group has-error"><label><input class="" id="-Name" name="Name" type="checkbox" value="true" /> Custom</label><span class="help-block">Name shoud be AJ.</span></div>`, l.String())
r.Equal(`<div class="form-group has-error"><label><input class=" is-invalid" id="-Name" name="Name" type="checkbox" value="true" /> Custom</label><div class="invalid-feedback help-block">Name shoud be AJ.</div></div>`, l.String())
}

type Person struct {
Expand Down

0 comments on commit ad62447

Please sign in to comment.