I have a set of items that I can edit and delete with a form. Now, all itms are displayed in a list as fieldsets. The fieldsets are created with this method:
$venues = db_query('SELECT vid, name, address, postcode, city FROM venues v WHERE v.uid = :uid', array(':uid' => $user->uid));
if($venues->rowCount() != 0) {
foreach($venues as $venue) {
$page['venue_editing_form'][] = drupal_get_form('venue_editor_form', $venue->vid, $venue->name, $venue->address, $venue->postcode, $venue->city);
}
}
Then the form is created with this function:
function venue_editor_form($form, &$form_state, $vid, $vname, $vadd, $vpostc, $vcity) {
$form['#prefix'] = '<div class="vedit">';
$form['#suffix'] = '</div>';
$form['edit_fieldset_'.$vname] = array(
'#type' => 'fieldset',
'#title' => t($vname),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attributes' => array('class' => array('venue-edit')),
);
$form['edit_fieldset_'.$vname]['venue_name'] = array(
'#type' => 'textfield',
'#title' => t('Venue name:'),
'#description' => t('Enter the venue name.'),
'#default_value' => $vname,
'#attributes' => array('onfocus' => 'this.value=""')
);
$form['edit_fieldset_'.$vname]['address'] = array(
'#type' => 'textfield',
'#title' => t('Address:'),
'#description' => t('The address of this venue.'),
'#default_value' => $vadd,
'#attributes' => array('onfocus' => 'this.value=""')
);
$form['edit_fieldset_'.$vname]['postcode'] = array(
'#type' => 'textfield',
'#title' => t('Postcode'),
'#description' => t('Enter the postcode for this event, so planB can position it on the map'),
'#default_value' => $vpostc,
'#attributes' => array('onfocus' => 'this.value=""', 'onblur' => 'getLatLong(this)')
);
$form['edit_fieldset_'.$vname]['city'] = array(
'#type' => 'textfield',
'#title' => t('City'),
'#description' => t('The city this venue is in.'),
'#default_value' => $vcity,
'#attributes' => array('onfocus' => 'this.value=""')
);
$form['edit_fieldset_'.$vname]['edit_submit'] = array(
'#type' => 'submit',
'#value' => t('Submit changes'),
'#validate' => array('venue_creation_form_validate')
);
$form['edit_fieldset_'.$vname]['delete_venue'] = array(
'#type' => 'submit',
'#value' => t('Delete venue'),
'#submit' => array('venue_form_delete')
);
$form['edit_fieldset_'.$vname]['venueid'] = array(
'#type' => 'value',
'#value' => $vid
);
return $form;
}
And one of the submit handlers:
function venue_form_delete($form, &$form_state) {
$deleted = db_delete('venues')
->condition('vid', $form['#venueid'], '=')
->execute();
drupal_set_message('Venue deleted successfully.');
}
Now, the problem is that no matter which venue’s deletion button I press, Drupal always deletes the first one of the ones in the list. $form['#venueid'] is always set to the first venue in the list in the submit handler, for some reason, but if I print a message when the forms are being generated, each of the forms has a different ID, as it should be.
Has anyone had this problem before?
One of my co-workers ran into this a few weeks ago. The reason you are having this problem is because you are trying to load the same form multiple times on one page with different data. It seems like this should work, however it doesn’t because all the forms end up with the same token / ID. When you submit each form the system doesnt know which one you really clicked on and can not take the appropriate action.
There are 2 approaches to solve this problem:
1) Rework your forms so that it is all handled with 1 form instead of multiple
2) use hook_forms(…) to create a ‘base form’ and then generate unique forms for each venue that map to the ‘base form’. This will make sure each form gets a unique ID without having to write forms for each one.
I dont have a drupal 7 example but here is a page that shows how to solve your problem for drupal 6 using hook_forms http://www.computerminds.co.uk/drupal-6-multiple-instances-same-form-one-page . You should be able to apply the same technique
I havent tested this code, but this is the basic idea:
Modify your get_form to use a unique id
Impliment hook_forms
And then i think you have to modify your form’s submit parameter to make sure they all get routed to the same submit handler. Though it does look like you are adding different submits per button so you might not have to do anything there
You might also have to do the same with the validate functions.
That should do it.