Skip to content

Commit

Permalink
Merge pull request #127 from tatang26/task-revert-changes-from-checkb…
Browse files Browse the repository at this point in the history
…ox-tags

Revert Checkbox Tag changes
  • Loading branch information
paganotoni authored Nov 13, 2019
2 parents a37ad3c + 5860f64 commit 8469b49
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 242 deletions.
70 changes: 3 additions & 67 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="" /> 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="" /> 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 Expand Up @@ -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: `<input class="custom-input" id="-IsAdmin" name="IsAdmin" type="checkbox" value="true" />`,
},
{
Value: true,
name: "IsAdmin",
opts: tags.Options{
"tag_only": true,
"class": "custom-input",
},
output: `<input class="custom-input" id="-IsAdmin" name="IsAdmin" type="checkbox" value="true" checked />`,
},
{
Value: false,
name: "IsAdmin",
opts: tags.Options{},
output: `<div class="form-group"><label><input class="" id="-IsAdmin" name="IsAdmin" type="checkbox" value="true" /> IsAdmin</label></div>`,
},
{
Value: true,
name: "IsAdmin",
opts: tags.Options{},
output: `<div class="form-group"><label><input class="" id="-IsAdmin" name="IsAdmin" type="checkbox" value="true" checked /> IsAdmin</label></div>`,
},
{
Value: false,
name: "IsAdmin",
opts: tags.Options{
"unchecked": false,
},
output: `<div class="form-group"><label><input class="" id="-IsAdmin" name="IsAdmin" type="checkbox" value="true" /><input name="IsAdmin" type="hidden" value="false" /> IsAdmin</label></div>`,
},
}

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())
})
}
}
12 changes: 3 additions & 9 deletions form/checkbox_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down
41 changes: 0 additions & 41 deletions form/checkbox_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,3 @@ func Test_Form_CheckboxTag_WithLabel(t *testing.T) {
})
r.Equal(`<label><input name="Chubby" type="checkbox" value="true" /> check me</label>`, 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(`<label><input name="Chubby" type="checkbox" value="Custom Value" /></label>`, 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(`<input name="Chubby" type="checkbox" value="true" />`, 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(`<label><input name="Chubby" type="checkbox" value="" /></label>`, 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(`<input name="Chubby" type="checkbox" value="Cutsom Value" />`, ct.String())
}
26 changes: 0 additions & 26 deletions form/form_for.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
99 changes: 0 additions & 99 deletions form/form_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,102 +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" /></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" /></label>`,
},
{
Object: testObject{"Text"},
Options: tags.Options{},
Expected: `<label><input id="test-object-Field" name="Field" type="checkbox" value="Text" /></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="true" /></label>`,
},
{
Object: testObject{},
Options: tags.Options{
"tag_only": true,
},
Expected: `<input id="test-object-Field" name="Field" type="checkbox" value="true" />`,
},
{
Object: testObject{},
Options: tags.Options{
"tag_only": true,
},
Expected: `<input id="test-object-Field" name="Field" type="checkbox" value="true" />`,
},
{
Object: testObject{},
Options: tags.Options{
"checked": false,
"unchecked": true,
},
Expected: `<label><input id="test-object-Field" name="Field" type="checkbox" value="false" /><input name="Field" type="hidden" value="true" /></label>`,
},
{
Object: testObject{1},
Options: tags.Options{
"unchecked": 1,
},
Expected: `<label><input id="test-object-Field" name="Field" type="checkbox" value="1" /><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 8469b49

Please sign in to comment.