form icon indicating copy to clipboard operation
form copied to clipboard

Dynamic option values for a select?

Open cschep opened this issue 6 years ago • 1 comments

Hey thanks for this package! It's working well for most of my project, but I just wanted to be able to pass in a slice of ints or a strings or something and be able to generate option values for a select.. Any hints on how you might accomplish that? I'm kinda stumped. I can see how I could make them static by defining a "select" type, but it'd be cool if I could pass in a list too.

Thanks!

cschep avatar Mar 18 '19 03:03 cschep

Hmm...

The hard part here is this would result in a data type that isn't really two-directional, and right now all of the field types supported are two-directional. In case that doesn't make sense, let me give an example.

Imagine we had this type:

type Form struct {
  Email string
}

var f Form

With this we can both generate an input field named Email (let's call this encoding) as well as parse the form and place the Email input field's value into the f.Email field (decoding). Another side-benefit of this is that we can pre-fill form fields by setting f.Email BEFORE encoding, so if a user has an error on a form we can "remember" what they had input already.

Having slices generate select options would break that paradigm, so it might result in confusion. As a result, I am wondering if there might be a different way to approach this.

Do you mind if I take a few days to see what I can come up with? I have a few ideas I want try.

Eg one idea might be to make the field a string but do something like a csv of options with a special way to denote which was selected.

Another idea I want to experiment with is having two fields that are "linked". Eg maybe something like:

type Form struct {
  AgeRange string `form:"type=select"`
  AgeRangeOpts []string `form:"options_for=AgeRange"`
}

I'm not really sure which I'll prefer without a little more experimentation, but feedback is welcome 😄

joncalhoun avatar Mar 19 '19 13:03 joncalhoun