dalhorinek
Hello.
I’m working on a system with multiple different widgets in a dashboard. I don’t have widget specific schema, I would like to keep this only using a form definition as there is no need for any validation or complex logic.
But I’m struggling with adding a dynamic rows for a nested form, I can’t get it work with to_form functionality introduced in Phoenix 1.7.
my_form = to_form(%{
"some_key" => "123",
"properties" => [
%{ "key" => "some_user_key", "value" => "some_user_value" },
%{ "key" => "another_user_key", "value" => "another_user_value" }
]
})
And I’m trying to setup the form for the nested properties
I’ve tried something like
<.inputs_for :let={f_nested} field={@my_form[:properties]} default={[]}>
<.input name="key" field={f_nested[:key] />
<.input name="value" field={f_nested[:value] />
</.inputs_for>
But this doesn’t work, it works if the properties would be a map, not an array.
I assume I need to iterate over the values, but not use how. Do I need to use inputs_for function instead of the component and iterate over the result?
What is a correct way just using the to_form achieve this? Or do I need to use ecto schemas to achieve this?
Thank you in advance.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
japplegame
It’s a little late, but I ran into a similar problem and found a solution.
It expects two-element tuples in the list. The first element is used only for sorting, and the second is the data for the nested form:
Or you can use a map instead of a list (
defaultattribute ofinputs_forstill must be a list)wireless
Can you expand on this? I’m trying to do something similar. Whenever I pass a form to inputs_for it complains about the field not being one of embeds one, embeds_many,… . Ideally I just want to store a load of key value pairs in a map in the database. I don’t want the hassle of having to deal with embedded schemas.
arcanemachine
I think this may be what you’re looking for:
japplegame
Form:
Template: