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

String array should replace the existing one #42

Open
aight8 opened this issue Aug 2, 2019 · 3 comments
Open

String array should replace the existing one #42

aight8 opened this issue Aug 2, 2019 · 3 comments

Comments

@aight8
Copy link

aight8 commented Aug 2, 2019

Example:

type Form struct {
   Field []string `form:"field"`
}

form := Form{ Field: []string{"value1", "value2"} }

// vals is contains 'field': "value3", "value4"
_ := Decoder.Decode(&form, vals)

// form.Field contains now "value1", "value2", "value3", "value4".
// Excepted was only "value3", "value4"

If I decode the HTTP post values to this struct, the Field property gets appended by the values in the post data instead of replaced.

The use case is the following:
I pre-fill the Form struct with data based on the DB entity. I use this struct also in the view template to render the values.
If I would fill the Form struct after the decoding, it will override all the decoded fields if I not check every single field.

My logical expectation of the form decoder is:

  • If the form field exists in the values, make sure the value match the decoded value (now it's appending string array, don't tested other kind of arrays/struct array)
  • If not just leave the field how it is (works)
@deankarn
Copy link
Contributor

Hey @aight8 this was done on purpose so that the user had full control over the decoding process, I can see how it would be convenient for deeper nested structures to have this done automatically if desired though.

I can look into adding a decode option so that this behaviour can be configured via an option.

@AlisskaPie
Copy link

hi, has the option to configure the decoding behavior been added to the library?

@deankarn
Copy link
Contributor

deankarn commented Jun 3, 2023

@AlisskaPie no not as yet as I have been busy with other projects.

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

No branches or pull requests

3 participants