Skip to content

POST 请求中,一个 key 包含多个上传文件时,除了第一个文件,其他文件被丢弃,导致 BindSafeData 行为非预期。 #227

@hwbrzzl

Description

@hwbrzzl

System (please complete the following information):

  • OS: macOS
  • GO Version: 1.18
  • Pkg Version: 1.5.1

Describe the bug

POST 请求中,一个 key 包含多个上传文件时,除了第一个文件,其他文件被丢弃。导致 BindSafeData 只能绑定 File *multipart.FileHeader 类型,而无法绑定 File []*multipart.FileHeader 类型。

To Reproduce

type UserForm struct {
	Name string                  `validate:"required"`
	File []*multipart.FileHeader `validate:"required"`
}

data, err := validate.FromRequest(ctx.Request().Origin())
if err != nil {
	panic(err)
}

v := data.Create()
v.AddRule("name", "required")
v.AddRule("file", "required")

if v.Validate() { // validate ok
	// safeData := v.SafeData()
	userForm := &UserForm{}
	if err := v.BindSafeData(userForm); err != nil {
		panic(err)
	}

	// do something ...
	fmt.Println(userForm.File)
} else {
	fmt.Println(v.Errors)       // all error messages
	fmt.Println(v.Errors.One()) // returns a random error message text
}
curl --location '127.0.0.1:3000' \
--form 'name="test"' \
--form 'file=@"1.pdf"' \
--form 'file=@"2.pdf"'

Expected behavior

BindSafeData 支持绑定 File []*multipart.FileHeader 类型。

Screenshots

image image

Additional context

No

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingresolved

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions