phoenix_live_view_example icon indicating copy to clipboard operation
phoenix_live_view_example copied to clipboard

demo select bug

Open benwilson512 opened this issue 6 years ago • 1 comments

I have added :foo and :bar virtual columns to User. In the form, :foo is controlled by a selector with options A => a, B => b.

Also in the form, :bar is displayed with both a text field and a selector with the same options.

image

When I click on the selector to make a change to :foo, the validate callback in the live view also makes a change to :bar to set it the same as :foo: https://github.com/chrismccord/phoenix_live_view_example/pull/39/files#diff-ba686f7f18e067a11567b119ec1695e5R26

This is reflected in the :bar text field, but the :bar select tag does NOT change. If you IO.inspect the rendered form, you'll find that it is not sending selected on that option row for some reason.

image

This seems like a bug, since it makes it impossible to control select values from the backend. I'm happy to look into it but I'm having trouble finding where that logic exists.

benwilson512 avatar Aug 15 '19 12:08 benwilson512

OK I figured out why this is happening, but I don't know if what's happening is what should happen.

selected logic is here: https://github.com/phoenixframework/phoenix_html/blob/master/lib/phoenix_html/form.ex#L1121. If value is not set explicitly, selected/3 gives priority to the params https://github.com/phoenixframework/phoenix_html/blob/master/lib/phoenix_html/form.ex#L1131.

It only falls back to input_value if there are no params for that field name.

This means that I can get the behaviour I want by doing:

<%= select f, :bar, @options, value: Ecto.Changeset.get_field(f.source, :bar) %>

This is counterintuitive though because all generic inputs just use the input_value function, which puts priority on the changeset.

benwilson512 avatar Aug 15 '19 13:08 benwilson512