form icon indicating copy to clipboard operation
form copied to clipboard

String array should replace the existing one

Open aight8 opened this issue 6 years ago • 3 comments

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)

aight8 avatar Aug 02 '19 17:08 aight8

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.

deankarn avatar Aug 18 '19 16:08 deankarn

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

AlisskaPie avatar Jun 02 '23 11:06 AlisskaPie

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

deankarn avatar Jun 03 '23 05:06 deankarn