nathanvda / cocoon

Dynamic nested forms using jQuery made easy; works with formtastic, simple_form or default forms
http://github.com/nathanvda/cocoon
MIT License
3.08k stars 383 forks source link

[Feature Request] Automatically add empty fields #601

Closed Floppy closed 3 years ago

Floppy commented 3 years ago

This is a great gem, thank you! A request though: currently the user must click a link to add the field before filling it in. Could cocoon be configured to automatically create an empty field when there are none available, saving that click?

  1. User loads the form
  2. Cocoon adds an empty item automatically
  3. User fills in the empty item
  4. Cocoon automatically adds another empty item
  5. ... and so on

Can this already be done and I've just missed it? Or would this be a useful feature?

nathanvda commented 3 years ago

Well, yes this is already possible, but it requires some code of course.

Step 2 (adding an empty item) has been asked and answered many times, so we added it to our wiki

Secondly: "automatically adds another item" is hard to solve in a generic way (as being a part of cocoon imho), since it depends on how/when/which event you want to trigger the insertion of the new child form. But imho cocoon already has the required parts of solution. So in my idea a possible solution would look like this:

Does this sound reasonable?

If you need more practical example, with more code, it would help to already show more of your own code to start from.

Floppy commented 3 years ago

Ah, thanks, I'd not seen the wiki page, sorry. I'll see what I can work out based on your suggestions and add to the docs if I come up with anything that might be useful.