Use double equals for the select widget to allow for numeric values #56

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
2 participants

Because the select widget uses Object.keys(), it never reflects numerical values.

Given the following:

var form = forms.create({
  hours: fields.number({
    required: true,
    label: 'Hours',
    choices: _.range(24),
    widget: widgets.select()
  })
});

form.bind({hours: 10}).toHTML() will show 0 as selected.

Collaborator

ljharb commented Mar 4, 2013

Hmm - good catch, thanks!

I don't believe == should be used anywhere - I'm trying to come up with a better solution.

However, could you please provide one (or more!) test(s) (that fail on master, and pass with this change) so that I can test various solutions against it?

Collaborator

ljharb commented Mar 4, 2013

In addition, when would you be binding a numerical "10"? Submitted form values will always be strings, so you should be binding strings to them on the server in this example.

You may consider making choices an array of {key: "name", value: "Bob"} objects like jQueryUI does. That would solve this issue and allow for ordering to be preserved.

edit - used key instead of label, closer to what I meant

As for passing number values to bind, this does happen when passing saved data to the form for editing.

Collaborator

ljharb commented Mar 5, 2013

You've convinced me that it needs to handle numerical input. Changing or extending the API of choices is a future option, but before I change anything, I'd still need a test that's failing on master. If I have the time I'll try to make one, but it'd be much faster if you provided one yourself :-)

I'll give it a shot this evening.

Collaborator

ljharb commented Apr 30, 2013

_.range returns an array - the widget requires that choices be an object anyways, but regardless, since you can just do [1, 2, 3].map(String) before passing it in, I'm going to close this until I see a failing test.

@ljharb ljharb closed this Apr 30, 2013

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