carlosarli
Hello,
I wanted to ask you if you could point me in the right direction on how to do something that I’ve been banging my head against for a couple of days now. I have a table user which links with a has_many relationship to a table sibling that contains two fields name and surname. If I were to create a form that lets you add siblings and for each new sibling add the name and surname, how would you suggest I go on about doing it? What I mean is how can I add a “add” button to the user form that lets me add a new sibling every time i press it?
as of now I made the form multipart and added the inputs_for f, :siblings p nested form which lets me add a sibling per user.
I then followed an article from AlchemistCamp to make the form dynamic and added the add and remove buttton with relative js. problem is the article i followed is imprinted on adding only one field to fill an array structure rather than a separate model, so I was wondering if anybody had ever done that and could point me in the right direction.
Thank you very much
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 7 of 7 Posts
dokuzbir
I thnk you can achieve that via javascript. Check how phoenix creates inputs and add a event click listener to a button and append child to these to increase numbers each time. Also add
|> cast_assoc(:siblings)in your changeset aftercast.For example:
joaquinalcerro
Check this post. It might help.
Best regards,
Joaquín Alcerro
carlosarli
Thanks for your help
I solved it using the Formex library
it made things quite easy actually
vnz
Can you show your solution please?
carlosarli
Hi, yeah I’ll try to remember cause it has been a while
add this to your model
use Formex.Ecto.Schemaadd this to your schema
formex_collection_child()then you create a form type like so
in the controller you use the create form module you created
then in the template you use the helper methods provided
for more in depth and precise information the library page is really good formex github
I hope I have been able to help, as I mentioned it has been a while. Any questions just ask
Good luck!
alexandrubagu
If you don’t want to use
Formexhere’s an example of nested form. Assuming Userhas_oneProfile:Here’s the user repository for creating changesets:
Inside your controller you can do something like this:
and inside your template you can use the nested form:
Hope will help
feliperenan
If somebody is looking for a solution that uses only Javascript, I have just created this library that should do this work.
https://github.com/feliperenan/dynamic_nested
The code I have there is just a start after facing this problem again in my new project. So it’s just about adding/removing group of fields. Feel free to contribute if you have any ideas