Skip to content

feat: support multi multipart.FileHeader binding #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025

Conversation

almas1992
Copy link
Contributor

@almas1992 almas1992 commented Mar 29, 2025

close #227
close goravel/goravel#229

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 ...
	for k, v := range userForm.File {
             fmt.Println(k, "=====", v.Filename)
        }
} else {
	fmt.Println(v.Errors)       // all error messages
	fmt.Println(v.Errors.One()) // returns a random error message text
}
curl --location 'http://127.0.0.1:3000/files' \
--form 'name="test"' \
--form 'file=@"./docker-entrypoint.sh"' \
--form 'file=@"./start.sh"'

Print:

0 ===== docker-entrypoint.sh
1  ===== start.sh

@coveralls
Copy link

coveralls commented Mar 29, 2025

Pull Request Test Coverage Report for Build 14162087994

Details

  • 19 of 21 (90.48%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.06%) to 96.681%

Changes Missing Coverage Covered Lines Changed/Added Lines %
data_source.go 19 21 90.48%
Totals Coverage Status
Change from base Build 14049851125: -0.06%
Covered Lines: 2942
Relevant Lines: 3043

💛 - Coveralls

@inhere inhere added the enhancement New feature or request label Mar 29, 2025
@inhere inhere merged commit 3d8c39a into gookit:master Mar 31, 2025
10 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
3 participants