From 5860f643ee6ddb3f50ccb967a8b93ca888d12b48 Mon Sep 17 00:00:00 2001 From: Walderman Date: Wed, 13 Nov 2019 17:36:50 -0500 Subject: [PATCH] revert changes --- form/bootstrap/form_for_test.go | 70 +---------------------- form/checkbox_tag.go | 12 +--- form/checkbox_tag_test.go | 41 -------------- form/form_for.go | 26 --------- form/form_for_test.go | 99 --------------------------------- 5 files changed, 6 insertions(+), 242 deletions(-) diff --git a/form/bootstrap/form_for_test.go b/form/bootstrap/form_for_test.go index fea1337..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(`
Name shoud be AJ.
`, l.String()) + r.Equal(`
Name shoud be AJ.
`, l.String()) } type Person struct { @@ -304,7 +304,7 @@ func Test_Field_TagOnly(t *testing.T) { opts: tags.Options{ "tag_only": true, }, - output: ``, + output: ``, }, { @@ -334,67 +334,3 @@ func Test_Field_TagOnly(t *testing.T) { }) } } - -func Test_Field_Boolean(t *testing.T) { - model := struct { - IsAdmin bool `schema:"-"` - }{} - cases := []struct { - f func(field string, opt tags.Options) *tags.Tag - Value bool - name string - opts tags.Options - output string - }{ - { - Value: false, - name: "IsAdmin", - opts: tags.Options{ - "tag_only": true, - "class": "custom-input", - }, - output: ``, - }, - { - Value: true, - name: "IsAdmin", - opts: tags.Options{ - "tag_only": true, - "class": "custom-input", - }, - output: ``, - }, - { - Value: false, - name: "IsAdmin", - opts: tags.Options{}, - output: `
`, - }, - { - Value: true, - name: "IsAdmin", - opts: tags.Options{}, - output: `
`, - }, - { - Value: false, - name: "IsAdmin", - opts: tags.Options{ - "unchecked": false, - }, - output: `
`, - }, - } - - for index, tcase := range cases { - t.Run(fmt.Sprintf("%v", index), func(tt *testing.T) { - r := require.New(tt) - - model.IsAdmin = tcase.Value - f := bootstrap.NewFormFor(model, tags.Options{}) - - l := f.CheckboxTag(tcase.name, tcase.opts) - r.Equal(tcase.output, l.String()) - }) - } -} diff --git a/form/checkbox_tag.go b/form/checkbox_tag.go index 21c4055..b80255a 100644 --- a/form/checkbox_tag.go +++ b/form/checkbox_tag.go @@ -19,13 +19,7 @@ func (f Form) CheckboxTag(opts tags.Options) *tags.Tag { if checked == nil { checked = "true" } - - isChecked := template.HTMLEscaper(value) == template.HTMLEscaper(checked) - if isChecked || value == nil { - value = checked - } - - opts["value"] = value + opts["value"] = checked unchecked := opts["unchecked"] delete(opts, "unchecked") @@ -36,13 +30,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 && template.HTMLEscaper(value) == template.HTMLEscaper(checked) + ct.Checked = template.HTMLEscaper(value) == template.HTMLEscaper(checked) return ct } tag := tags.New("label", tags.Options{}) ct := f.InputTag(opts) - ct.Checked = isChecked && template.HTMLEscaper(value) == template.HTMLEscaper(checked) + ct.Checked = 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 bf46097..6d8c356 100644 --- a/form/checkbox_tag_test.go +++ b/form/checkbox_tag_test.go @@ -46,44 +46,3 @@ func Test_Form_CheckboxTag_WithLabel(t *testing.T) { }) r.Equal(``, ct.String()) } - -func Test_Form_CheckboxTag_With_Text_Value(t *testing.T) { - r := require.New(t) - f := form.New(tags.Options{}) - ct := f.CheckboxTag(tags.Options{ - "value": "Custom Value", - "name": "Chubby", - }) - r.Equal(``, ct.String()) -} - -func Test_Form_CheckboxTag_Tag_Only(t *testing.T) { - r := require.New(t) - f := form.New(tags.Options{}) - ct := f.CheckboxTag(tags.Options{ - "tag_only": true, - "name": "Chubby", - }) - r.Equal(``, ct.String()) -} - -func Test_Form_CheckboxTag_With_Empty_Text_Value(t *testing.T) { - r := require.New(t) - f := form.New(tags.Options{}) - ct := f.CheckboxTag(tags.Options{ - "value": "", - "name": "Chubby", - }) - r.Equal(``, ct.String()) -} - -func Test_Form_CheckboxTag_TagOnly_With_CustomValue(t *testing.T) { - r := require.New(t) - f := form.New(tags.Options{}) - ct := f.CheckboxTag(tags.Options{ - "tag_only": true, - "value": "Cutsom Value", - "name": "Chubby", - }) - r.Equal(``, ct.String()) -} diff --git a/form/form_for.go b/form/form_for.go index ee71ccb..1bac915 100644 --- a/form/form_for.go +++ b/form/form_for.go @@ -74,36 +74,10 @@ 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.buildCheckboxOptions(field, opts) f.buildOptions(field, opts) - return f.Form.CheckboxTag(opts) } -func (f FormFor) buildCheckboxOptions(field string, opts tags.Options) { - fn := f.reflection.FieldByName(field) - ov := opts["value"] - if fn.Kind() == reflect.Slice || fn.Kind() == reflect.Array && ov != nil { - s := reflect.ValueOf(fn.Interface()) - for i := 0; i < s.Len(); i++ { - if reflect.DeepEqual(s.Index(i).Interface(), ov) { - opts["checked"] = s.Index(i) - break - } - } - } - - if fn.Kind() == reflect.Bool { - if ov == nil { - opts["value"] = true - } - - if opts["checked"] == nil && !fn.Bool() { - opts["checked"] = fn.Bool() - } - } -} - // 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 eb990b1..a00000c 100644 --- a/form/form_for_test.go +++ b/form/form_for_test.go @@ -316,102 +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{}, - Options: tags.Options{ - "tag_only": true, - }, - Expected: ``, - }, - { - Object: testObject{}, - Options: tags.Options{ - "tag_only": true, - }, - Expected: ``, - }, - { - Object: testObject{}, - Options: tags.Options{ - "checked": false, - "unchecked": true, - }, - 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) - } - -}