From 7184d932ba099072807fd74c8ff3a8c2fe4949f0 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Mon, 18 Dec 2017 12:49:48 -0500 Subject: [PATCH] added a FileTag and made sure the form automatically flips to multipart --- form/file_tag.go | 13 +++++++++++++ form/input_tag.go | 3 +++ form/input_tag_test.go | 8 ++++++++ 3 files changed, 24 insertions(+) create mode 100644 form/file_tag.go diff --git a/form/file_tag.go b/form/file_tag.go new file mode 100644 index 0000000..01ebd08 --- /dev/null +++ b/form/file_tag.go @@ -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) +} diff --git a/form/input_tag.go b/form/input_tag.go index 5bc0382..b8067e8 100644 --- a/form/input_tag.go +++ b/form/input_tag.go @@ -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) } diff --git a/form/input_tag_test.go b/form/input_tag_test.go index f6a9443..f274935 100644 --- a/form/input_tag_test.go +++ b/form/input_tag_test.go @@ -28,3 +28,11 @@ func Test_Form_DateTimeTag(t *testing.T) { }) r.Equal(``, 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(``, i.String()) + r.Equal(f.Options["enctype"], "multipart/form-data") +}