How to implement "Add another item" for custom forms?
I am writing a module which is basically a form that creates a relation between two end points.
I want a user to be able to create multiple relations at once. I want to add a button which says "add another item" just like how fields have when you select unlimited in their settings.
Is there a way I can do this to custom forms too ?
Answers 2
One way to do this is by setting the form fields/field sets within a for function. Create a seperate function to increment the value via a submit button callback for instance and rebuild the form after you increment.
Here is code from form_example_tutorial_9 which shows information on how to create dynamic fields. This information is much more thorough than my explanation:
http://api.drupal.org/api/examples/form_example!form_example_tutorial.inc/function/form_example_tutorial_9_add_name/7
http://api.drupal.org/api/examples/form_example%21form_example_tutorial.inc/function/form_example_tutorial_9/7
To have a simple and working code example:
Drupal 8
Check out Example module's
AjaxAddMore.php
(click the small "View source" link).