New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass ecto models to multiple_select #32
Comments
Just filter the data. In your controller:
There is no way Ecto nor Phoenix can guess which fields we should use. Better to be explicit. You can also use a Enum.map later:
|
Thanks for your quick reply. The other form helpers like text_input set their value automatically from the form model. With multiple_select I have to pass in the current values manually (and transform them again) else the options won't be selected: <%= multiple_select f, :users, @all_users, value: Enum.map(@changeset.model.users, fn(u) -> u.id end) %> But when the form is rendered because of an error, the value property should be filled in with the current In Rails the helper assumes some default names for the fields, but it is possible to override these: collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) |
I see the issue now. The issue is that we are not correctly marking the proper fields as chosen when the data comes through the form. In any case, I would still not support the
I know. :D We wrote it in simple_form before it was merged into Rails ;) |
Correct! And we have to differentiate between rendering the form through the
I hope this is understandable :-D If not I can try to make a demo project... When Ecto supports inserting rows in the mapping table for many-to-many relationships this form feature would make it easier to handle these cases.
Haha right! If I remember correctly I read somewhere that you were part of the Rails core team, nice job man! 👍 |
You can use the |
I actually, I have just verified this is working as expected. You just want to use the default option when giving the default values from the model. :) |
Oh right the default option, did not see that. Sorry! However I am not sure if I have to use So I am currently experimenting with the following code: <%= multiple_select f, :users,
# fetched with a query select: {u.name, u.id}
@all_users,
# select the options in case of an error else it is nil and the defaults are used
value: (@changeset.params || %{}) |> Map.get("users"),
# or uses values?
#values: (@changeset.params || %{}) |> Map.get("users"),
# when value(s) is null use the mapping from the model (open the form for the first time)
default: Enum.map(@changeset.model.users, &(&1.id)),
class: "form-control" %> This will not select the rows when opening the form for the first time, when the value property is nil (without a submit). It is working when I use Thanks for your help! |
I have just tried out your latest refactoring and I think the problem is that {default, opts} = Keyword.pop(opts, :default, [])
{multiple, opts} = Keyword.pop(opts, :value, default) will return nil for
|
No, you just need
|
Haha okay I did make it more complicated :-D |
No, you are right, we need to fix this. |
Ok, let me know if I can help or test something. I will take a look at the code as well. |
I am currently playing around with the phoenix framework and I like the similarities to rails!
One thing that I think is still missing is the proper handling of many to many relationships. Regarding the changes required in the ecto framework there is already a ticket: elixir-ecto/ecto#183
I think another change would be to allow the multiple_select helper to work with ecto models. So that we can do something like this:
And the form helper could fetch all currently assigned users using the
value_from(form, field)
method and set the "selected" property on the option elements. In Rails there is the http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_check_boxes to handle this case, but I think it is much nicer to use a multiple select so you can do autocomplete input fields.The text was updated successfully, but these errors were encountered: