Skip to content
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

Issue Decoding Blank Values #45

Closed
deankarn opened this issue Aug 12, 2015 · 3 comments
Closed

Issue Decoding Blank Values #45

deankarn opened this issue Aug 12, 2015 · 3 comments

Comments

@deankarn
Copy link

Situation

Have a user struct, populated via a database query that includes a LastName field already populated
We have a user update HTML form which posts the updates.

Issue

When LastName, or other fields, are set to blank in the HTML input the form posts the following:

map[IsActive:[true] FirstName:[Joey] LastName:[] PreferredFirstName:[Joey] Username:[joeybloggs]]

this is correct as you can see LastName has no value; however after running schema decoder like so:

// user originally grabbed from DB in middleware and set on context ( using gorilla contaxt )
user := context.Get(r, "user").(*User)

if err = schemaDecoder.Decode(user, r.MultipartForm.Value); err != nil {
  log.Println(err.Error())
  http.Error(w, "Invalid Request", http.StatusBadRequest)
  return
}

fmt.Println(user.LastName) // outputs Bloggs, the fields original value

LastName was not set to blank and still retains it's original value.

My specific problem

the user struct is validated after schema decoder runs and in this situation validation should report an error, however it all passes, user gets saved to the database and user is notified that the user saved successfully.

Notes

This is not the same issue as #17 as I don't want to zero out fields not posted.

@kisielk
Copy link
Contributor

kisielk commented Aug 13, 2015

I think the option in #17 should do what you need. It doesn't deal with fields not posted, but fields which were posted but had an empty value.

@deankarn
Copy link
Author

OK sorry I must have misread, will set the flag and confirm in the morning

Thanks!

@deankarn
Copy link
Author

Confirmed this solves the problem, Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants