Wigny
Hey folks,
I have a problem that does not seem to have a straightforward solution to me, but it feels like it should have one:
How should we go about saving a form (adding/removing/updating entries) that produces a list of changesets, makes the operations in the DB and then refreshes the form with the new values?
I know that is possible when the form is based on a schema that has an embeds_many or has_many, as we have a guide in LiveView docs and also the excellent One-to-Many LiveView Form | Benjamin Milde article from @lostkobrakai, but given I would like to change multiples entries that doesn’t belongs_to a unique schema, how can I put this together inside a single form?
This question comes from a page that I’m trying to build that should allow the user to add a new entry in the DB, update an existing one or delete it, like in a bulk operation. Also, the table should have constraints, and therefore I thought using changesets would make it easy to handle and return error messages for inputs.
I’ve put a gist that goes into the direction of what I want, but I’m stuck in saving the list of changesets and refreshing the form based on this list:
How would you tackle that? Would you simply not use form to solve this problem?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #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
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
Why do you think you cannot create one? It could even live just in the controller/live view rendering your form.
Wigny
I believe I’m trying this approach with the
MailingListembedded schema here. The thing is that I want to insert/update/delete only the changeset list ofemails. Given I can grab it withget_embed(mailing_list, :emails), I tried iterating over the list of emails and applying them to the DB, but I could not figure out how to apply them back in theMailingListchangeset.Is having a
MailingListembedded schema what you had in mind?codeanpeace
Have you tried
put_embed/4from Ecto.Changeset — Ecto v3.14.0?Wigny
Yes, I have. I think the form wasn’t behaving correctly when I tried it because I was calling
put_embedafter the changesets being applied to the DB, which caused them to have the actionreplaceand it left the form in a wrong state.I guess I found a workaround for that, which is to reinitialise the original changeset instead of trying to update it after applying the operations to the DB. The code is something like this now: