Hey!
I'd like to have multiple forms on a single page. But in the form_state there's always just the information from the first form on the page.

I've got a small form function:

function module_form($form, &$form_state, $file) {
  $form[$file]['file'] = array(
    '#type' => 'textfield',
    '#value' => $file, 
    '#title' => 'file', 
  );
  $form[$file]['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Submit'),
  );
  return $form;
}

This function is called from another function like this:

...
	foreach($files as $file) {
		$form = drupal_get_form('module_form', $file->uri);
		$output .= render($form);
	}
...

What am I doing wrong?

Comments

nevets’s picture

Each form has to have a unique id, see hook_form().

Though I wonder why you do not simply use a single form that supports multiple fields.

derMatze’s picture

hook_forms is the solution, thanks!

I do have a lot (hundreds) of elements (= forms) on that one page. I don't want the user to send them all everytime one item is updated.

gusantor’s picture

Can you post a working example ?

I'm trying to get this, multiple forms of one content type, "saldo_dias_feriado", on one page ... no luck

what I've done is also on this question, I resume

at hook_menu()

$items['user/%user/administracion/saldos-vacaciones-admin'] = array(
    'type'=>MENU_LOCAL_TASK,
    'title'=>'Saldos admin',
    'page callback'=>'pezgordo_saldos_dias_feriado_form',
    'page arguments' => array(1),
    'access arguments' => TRUE,
    'file' => 'saldos_dias_feriado_form.inc',
    'file path' => drupal_get_path('module', 'pezgordo').'/includes/user',
);

at saldos_dias_feriado_form.inc

function pezgordo_saldos_dias_feriado_form($persona=NULL){

    //this is an array of nodes for which I want to generate the forms
    $saldos = $array_of_nodes;

    if($saldos){
        foreach($saldos as $ks=>$s){
            $form_state[$ks]['build_info']['args'] = array($s);
            
            $form_load_include($form_state[$ks], 'inc', 'node', 'node.pages');

            $form[$ks] = drupal_get_form('saldo_dias_feriado_node_form_'.$s->nid, $s->nid);

        }//fin para cada saldo
    }//fin hay saldos

    return $form;
}

and at pezgordo.module()

function pezgordo_forms($form_id, $args){
  $forms = array();
  // Check if the form id is of the pattern add_to_collection_form-[NUMBER]
  if(preg_match('/^saldo_dias_feriado_node_form_\d+$/', $form_id)){
    dpm($form_id);
    $forms['$form_id']['callback'] = 'saldo_dias_feriado_node_form';
  }
  return $forms;
}

but this, and some other attempts with subtle variants doesn't ork for me, results vary from:

  • always updating the node related to first form
  • getting the error message
    El contenido de esta página ha sido modificado por otro usuario o usted ya ha enviado modificaciones con este mismo formulario. En consecuencia, sus cambios no se pueden guardar.
  • no form render

please help ! , thanks