diff --git a/form/bootstrap/form_for_test.go b/form/bootstrap/form_for_test.go
index c13c289..bcfa509 100644
--- a/form/bootstrap/form_for_test.go
+++ b/form/bootstrap/form_for_test.go
@@ -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(`
`, l.String())
+ r.Equal(``, l.String())
}
func Test_InputError(t *testing.T) {
@@ -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(``, l.String())
+ r.Equal(``, l.String())
}
type Person struct {
@@ -304,7 +304,7 @@ func Test_Field_TagOnly(t *testing.T) {
opts: tags.Options{
"tag_only": true,
},
- output: ``,
+ output: ``,
},
{
diff --git a/form/checkbox_tag.go b/form/checkbox_tag.go
index 33ea0d8..cb349db 100644
--- a/form/checkbox_tag.go
+++ b/form/checkbox_tag.go
@@ -13,35 +13,21 @@ 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")
@@ -49,13 +35,13 @@ func (f Form) CheckboxTag(opts tags.Options) *tags.Tag {
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 {
diff --git a/form/checkbox_tag_test.go b/form/checkbox_tag_test.go
index 45411bf..0bff1b4 100644
--- a/form/checkbox_tag_test.go
+++ b/form/checkbox_tag_test.go
@@ -74,18 +74,7 @@ func Test_Form_CheckboxTag_With_Empty_Text_Value(t *testing.T) {
"value": "",
"name": "Chubby",
})
- r.Equal(``, 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(``, ct.String())
+ r.Equal(``, ct.String())
}
func Test_Form_CheckboxTag_TagOnly_With_CustomValue(t *testing.T) {
diff --git a/form/form_for.go b/form/form_for.go
index 50a82b2..1bac915 100644
--- a/form/form_for.go
+++ b/form/form_for.go
@@ -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)
diff --git a/form/form_for_test.go b/form/form_for_test.go
index 02795df..a00000c 100644
--- a/form/form_for_test.go
+++ b/form/form_for_test.go
@@ -316,80 +316,3 @@ func Test_FormFor_DateTimeTag(t *testing.T) {
i := f.DateTimeTag("BirthDate", tags.Options{})
r.Equal(``, 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(``, 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(``, 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: ``,
- },
- {
- Object: testObject{"Text"},
- Options: tags.Options{},
- Expected: ``,
- },
- {
- Object: testObject{true},
- Options: tags.Options{},
- Expected: ``,
- },
- {
- Object: testObject{""},
- Options: tags.Options{},
- Expected: ``,
- },
- {
- Object: testObject{1},
- Options: tags.Options{
- "unchecked": 1,
- },
- Expected: ``,
- },
- }
-
- 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)
- }
-
-}