Skip to content

Commit

Permalink
added a FileTag and made sure the form automatically flips to multipart
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Dec 18, 2017
1 parent 496f2d0 commit 7184d93
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions form/file_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package form

import "github.com/gobuffalo/tags"

func (f Form) FileTag(opts tags.Options) *tags.Tag {
if opts["type"] == nil {
opts["type"] = "file"
}
if opts["type"] == "file" {
f.Options["enctype"] = "multipart/form-data"
}
return f.InputTag(opts)
}
3 changes: 3 additions & 0 deletions form/input_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ func (f Form) InputTag(opts tags.Options) *tags.Tag {
if opts["type"] == nil {
opts["type"] = "text"
}
if opts["type"] == "file" {
f.Options["enctype"] = "multipart/form-data"
}
return tags.New("input", opts)
}

Expand Down
8 changes: 8 additions & 0 deletions form/input_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ func Test_Form_DateTimeTag(t *testing.T) {
})
r.Equal(`<input type="datetime-local" value="1976-08-24T06:17" />`, i.String())
}

func Test_Form_InputTag_File(t *testing.T) {
r := require.New(t)
f := form.New(tags.Options{})
i := f.InputTag(tags.Options{"type": "file"})
r.Equal(`<input type="file" />`, i.String())
r.Equal(f.Options["enctype"], "multipart/form-data")
}

0 comments on commit 7184d93

Please sign in to comment.