monoculum / formam

a package for decode form's values into struct in Go
Apache License 2.0
190 stars 19 forks source link

How to use not indexed arrays? #7

Closed mrLSD closed 5 years ago

mrLSD commented 7 years ago

How to use not indexed arrays?

From your example, when I trying change:

  <input type="text" name="Founders[0]" value="Masaru Ibuka"/>
  <input type="text" name="Founders[1]" value="Akio Morita"/>

to

  <input type="text" name="Founders[]" value="Masaru Ibuka"/>
  <input type="text" name="Founders[]" value="Akio Morita"/>

I got: runtime error: slice bounds out of range

emilgpa commented 7 years ago

@mrLSD removing "[]" should works!

mrLSD commented 7 years ago

@dashaus, Thank you for the response and great library!

In Usage section:

  • Use [] for access to index of a slice/array. (i.e, struct.array[0]). If the array/slice is the last field of the path, it is not necessary to indicate the index

as I understand, and it's common practice: "field[]" Without "[]" it work pretty well.

But as I mentioned before: it's common and useful practice: "field[]" because it is indicating Array.

Do you plan implement that?

emilgpa commented 7 years ago

I can implement it if this not affects to the performance (I think that it no should to affect). In the next days, I go do it!

mrLSD commented 7 years ago

Thank you for the response! I have additional question, but off-topic. I don't know, is it about new issue: How to fetch errors to end-user? For example:

    dec := formam.NewDecoder(&formam.DecoderOptions{TagName: "formam"})
    if err := dec.Decode(values, &company); err != nil {
        fmt.Printf("Error: %#v\n\n", err.Error())
    }

But I need notify HTML form about problem:

Because err.Error() return only string with first error.

emilgpa commented 7 years ago

The error message produced by formam should be useful for the developer, not for end-user, so when the decoder fails I think that you should to display a 500 status error, but if the error is because the user introduces incorrect input then should be validated by a validation package like monoculum/valider but my package is not finished so I don't recommend used it until it finished and to use another validation package.

ergoz commented 7 years ago

@mrLSD Try to set tag in your struct:

`formam:"foundation[]"`

Maybe it will help