Posted by dmitriy.trt on April 7, 2010 at 11:31am
I've almost finished module to provide new Form API element (Dynarray), which generates dynamic elements array. For example, you want to have fieldset with Cars, each car has 3 fields (model, color, country) and you don't know number of cars, users should add as many cars as they want.
With dynarray it will be simple, you'll need to define form element:
<?php
$form['cars'] = array(
'#type' => 'dynarray',
'#display_type' => 'fieldset', //Optional
'#title' => t('Cars'),
'#min' => 0, //Default minimum is 1, but we assume not all people have a car
'#max' => 5, //Limit cars number to 5
'#item' => array( //Single element definition, you can write ANY form elements here, even nested trees, fieldsets and even more dynarrays!
'#type' => 'fieldset',
'#title' => t('One car'),
'model' => array(
'#type' => 'textfield',
'#title' => t('Model'),
),
'color' => array(
'#type' => 'select',
'#title' => t('Color'),
'#options' => array(...),
),
'country' => array(
'#type' => 'textfield',
'#title' => t('Country'),
),
),
);
?>This will result one fieldset with button 'Add', which adds more elements through AHAH, all elements (except those shouldn't be deleted according to #min property) will have button 'Delete'.
You will be able to define #default_value array and there will be elements created and filled with this default values.
Also, it will be possible to define #item_generator property with function (default_value will be an argument for this function), which should return Form API definition of one item.
Am I writing a duplicate of existing module? Is there anyone working on similar modules?...

Comments
Not exactly the same, but
Not exactly the same, but have you taken a look at the Multigroup module which is part of CCK 3.x? It allows pretty much the same, just without coding, i.e. everything is done with fields and within the CCK UI.
-Stefan
Thanks, I'll take a look at
Thanks, I'll take a look at it.
What Dmitriy is working on is
What Dmitriy is working on is still needed, for use cases (and there are many) where people want to use form api, and do not want to cck...
Sam Rose
Hollymead Capital Partners
P2P Foundation
Social Media Classroom
After some experimentation, I
After some experimentation, I realized http://drupal.org/project/ahah_helper (as of this writing version DRUPAL-6--2 in cvs) definitely does this via form api.
An example that I was able to get working is here: http://jamestombs.co.uk/2010-07-08/adding-additional-form-elements-using...
Sam Rose
Hollymead Capital Partners
P2P Foundation
Social Media Classroom