Skip to content

Commit

Permalink
Merge pull request #124 from gobuffalo/revert-123-bugfix-checkbox-tag…
Browse files Browse the repository at this point in the history
…-form-for

Revert "WIP: Fix checkboxTag for Form_for"
  • Loading branch information
paganotoni authored Oct 8, 2019
2 parents 7ceea25 + 6b8b92c commit 9ed2f79
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 127 deletions.
6 changes: 3 additions & 3 deletions form/bootstrap/form_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func Test_CheckBox(t *testing.T) {
r := require.New(t)
f := bootstrap.NewFormFor(struct{ Name string }{}, tags.Options{})
l := f.CheckboxTag("Name", tags.Options{"label": "Custom"})
r.Equal(`<div class="form-group"><label><input class="" id="-Name" name="Name" type="checkbox" value="" /><input name="Name" type="hidden" value="" /> Custom</label></div>`, l.String())
r.Equal(`<div class="form-group"><label><input class="" id="-Name" name="Name" type="checkbox" value="true" /> Custom</label></div>`, l.String())
}

func Test_InputError(t *testing.T) {
Expand Down Expand Up @@ -229,7 +229,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=" is-invalid" id="-Name" name="Name" type="checkbox" value="" /><input name="Name" type="hidden" value="" /> Custom</label><div class="invalid-feedback help-block">Name shoud be AJ.</div></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 Expand Up @@ -304,7 +304,7 @@ func Test_Field_TagOnly(t *testing.T) {
opts: tags.Options{
"tag_only": true,
},
output: `<input class="" id="-Name" name="Name" type="checkbox" value="" />`,
output: `<input class="" id="-Name" name="Name" type="checkbox" value="true" />`,
},

{
Expand Down
30 changes: 8 additions & 22 deletions form/checkbox_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,35 @@ func (f Form) CheckboxTag(opts tags.Options) *tags.Tag {

value := opts["value"]
delete(opts, "value")
if value != nil {
opts["value"] = value
if value == nil || value == "" {
value = "true"
}
opts["value"] = value

checked := opts["checked"]
delete(opts, "checked")
if checked == nil {
checked = "true"
}

if value == nil {
opts["value"] = checked
isChecked := checked != nil
if !isChecked {
checked = "true"
}

isChecked := template.HTMLEscaper(value) == template.HTMLEscaper(checked)

unchecked := opts["unchecked"]
delete(opts, "unchecked")
if unchecked != nil {
isUnchecked := template.HTMLEscaper(value) == template.HTMLEscaper(unchecked)
if isUnchecked {
isChecked = false

if value != "" {
delete(opts, "value")

}
}
}

hl := opts["hide_label"]
delete(opts, "hide_label")

if opts["tag_only"] == true {
delete(opts, "label")
ct := f.InputTag(opts)
ct.Checked = isChecked
ct.Checked = isChecked && template.HTMLEscaper(value) == template.HTMLEscaper(checked)
return ct
}

tag := tags.New("label", tags.Options{})
ct := f.InputTag(opts)
ct.Checked = isChecked
ct.Checked = isChecked && template.HTMLEscaper(value) == template.HTMLEscaper(checked)
tag.Append(ct)

if opts["name"] != nil && unchecked != nil {
Expand Down
13 changes: 1 addition & 12 deletions form/checkbox_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,7 @@ func Test_Form_CheckboxTag_With_Empty_Text_Value(t *testing.T) {
"value": "",
"name": "Chubby",
})
r.Equal(`<label><input name="Chubby" type="checkbox" value="" /></label>`, ct.String())
}

func Test_Form_CheckboxTag_With_Empty_Text_Value_Unchecked(t *testing.T) {
r := require.New(t)
f := form.New(tags.Options{})
ct := f.CheckboxTag(tags.Options{
"name": "Chubby",
"value": "",
"unchecked": "",
})
r.Equal(`<label><input name="Chubby" type="checkbox" value="" /><input name="Chubby" type="hidden" value="" /></label>`, ct.String())
r.Equal(`<label><input name="Chubby" type="checkbox" value="true" /></label>`, ct.String())
}

func Test_Form_CheckboxTag_TagOnly_With_CustomValue(t *testing.T) {
Expand Down
13 changes: 0 additions & 13 deletions form/form_for.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,9 @@ func loadErrors(opts tags.Options) *validate.Errors {
// CheckboxTag creates a checkbox for a field on the form Struct
func (f FormFor) CheckboxTag(field string, opts tags.Options) *tags.Tag {
f.buildOptions(field, opts)
f.validateCheck(field, opts)

return f.Form.CheckboxTag(opts)
}

func (f FormFor) validateCheck(field string, opts tags.Options) {
fv := f.value(field)
if fv != nil && fv != "" {
opts["checked"] = fv
return
}

opts["unchecked"] = fv
delete(opts, "checked")
}

// InputTag creates an input for a field on the form Struct
func (f FormFor) InputTag(field string, opts tags.Options) *tags.Tag {
f.buildOptions(field, opts)
Expand Down
77 changes: 0 additions & 77 deletions form/form_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,80 +316,3 @@ func Test_FormFor_DateTimeTag(t *testing.T) {
i := f.DateTimeTag("BirthDate", tags.Options{})
r.Equal(`<input id="-BirthDate" name="BirthDate" type="datetime-local" value="1976-08-24T06:17" />`, i.String())
}

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

p := struct {
IsAdmin bool
}{
IsAdmin: true,
}

f := form.NewFormFor(p, tags.Options{})
i := f.CheckboxTag("IsAdmin", tags.Options{})
r.Equal(`<label><input id="-IsAdmin" name="IsAdmin" type="checkbox" value="true" checked /></label>`, i.String())
}

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

p := struct {
UserType string
}{
UserType: "ADMIN",
}

f := form.NewFormFor(p, tags.Options{})
i := f.CheckboxTag("UserType", tags.Options{})
r.Equal(`<label><input id="-UserType" name="UserType" type="checkbox" value="ADMIN" checked /></label>`, i.String())
}

type testObject struct {
Field interface{}
}

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

cases := []struct {
Object testObject
Options tags.Options
Expected string
}{
{
Object: testObject{1},
Options: tags.Options{},
Expected: `<label><input id="test-object-Field" name="Field" type="checkbox" value="1" checked /></label>`,
},
{
Object: testObject{"Text"},
Options: tags.Options{},
Expected: `<label><input id="test-object-Field" name="Field" type="checkbox" value="Text" checked /></label>`,
},
{
Object: testObject{true},
Options: tags.Options{},
Expected: `<label><input id="test-object-Field" name="Field" type="checkbox" value="true" checked /></label>`,
},
{
Object: testObject{""},
Options: tags.Options{},
Expected: `<label><input id="test-object-Field" name="Field" type="checkbox" value="" /><input name="Field" type="hidden" value="" /></label>`,
},
{
Object: testObject{1},
Options: tags.Options{
"unchecked": 1,
},
Expected: `<label><input id="test-object-Field" name="Field" type="checkbox" /><input name="Field" type="hidden" value="1" /></label>`,
},
}

for idx, c := range cases {
f := form.NewFormFor(c.Object, tags.Options{})
i := f.CheckboxTag("Field", c.Options)
r.Equalf(c.Expected, i.String(), "Loop %d", idx+1)
}

}

0 comments on commit 9ed2f79

Please sign in to comment.