Hi,

After hours of searching in forums and trying different things, i finally came to think that the best is to ask here about my problem.
I'm a beginner in drupal API but i'm making programs in my professional life since many years

I want to create an edit form containing values from different request in database and that a user could change through the form. The idea is to show it in a table form.

Something that would look like :

row1  | Label1        | textfield1  | textfield2     | label2    |
row2  | Label1        | textfield1  | textfield2     | label2    |
...

Problème is #defaut_value from textfield is not shown and when i enter a value in the textfield, i cannot get #value as it is empty

If i use #value in code, values are visible but user input does not change #value when i get them from $form_state

Problem seems linked to the fact that my fields are in an array rendered to a table.
When i use textfields in the form, it is ok.

I have the problem with textfields or textarea inside a table. No problem with items , their default_value is well rendered.

Here are some parts of my code , i changed some names as i don't want them to be shown here :

Menu to define an access to my form

/**
 * Implements hook_menu().
 */
function mymodule_menu() {
  $items['admin/mymodule/%itemid/edit'] = array(
    'title' => 'Edit',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_edit_form', 3),
    'access arguments' => array('mymodule_administer'),
    'weight' => 5,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  );
  return $items;

}

Then my function to build my form


/**
* Edit facture form
*/
function mymodule_edit_form($form, &$form_state, $mydata) {

  //Item id
  $form['myitem_id'] = array(
    '#type' => 'hidden',
    '#default_value' => $mydata->myitem_id,
  );

  $form['destination'] = array(
    '#type' => 'hidden',
    '#value' => drupal_get_destination(),
  );

  //Table header
  $mytable_row_headers = array();
  $mytable_row_headers[] = array('data' => t('item1'), 'field' => 'item1');
  $mytable_row_headers[] = array('data' => t('item2'), 'field' => 'item2');
  $mytable_row_headers[] = array('data' => t('item3'), 'field' => 'item3');
  $mytable_row_headers[] = array('data' => t('item4'), 'field' => 'item4');  

  //Table data
  $mytable_rows_data = array();
  foreach($mydata->rows_data as one_row_data) {
     $mytable_rows_data[] = mymodule_build_row($one_row_data);
  }
  
  $form['fact_rows'] = array(
    '#prefix' => '<div id"my_rows">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#theme' => 'table',
    '#header' => $mytable_row_headers,
    '#rows' => $mytable_rows_data,
    '#element_validate' => array('mymodule_rows_validate'),
  );
  
  //One test textfield to check if #default_value is working
  $form['textfield_test'] = array(
      '#type' => 'textfield',
      '#default_value' => 'Test',
      '#element_validate' => array('mymodule_textfield_validate'),
      )
  ;

  //Submit button
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  field_attach_form('mymodule', $mydata, $form, $form_state);

  return $form;
}



function mymodule_build_row($one_row_data) {

  $one_row['item1']    = array('data' => array(
                                 '#type' => 'item' ,
                                 '#markup' => $one_row_data['item1_data'],
                                ));
  $one_row['item2'] = array('data'  => array(
                                 '#type'      => 'textfield',								 
                                 '#default_value'     => $one_row_data['item2_data']),
                                 '#size' =>10,                                 
                                 '#required' => TRUE,
                                 '#field_suffix' => => $one_row_data['item2_suffix']),
                                ));
  $one_row['item3']   = array('data' => array(
                                 '#type' => 'textfield',                                      
                                 '#default_value'  => $one_row_data['item3_data']),
                                 '#size' => 3,
                                 '#required' => TRUE,
                                ));
  $one_row['item4']     = array('data' => array(
                                '#type' => 'item',
                                '#markup' => => $one_row_data['item4_data']),                                
                          ));
  return $one_row;

Table array is rendered fine, and item1 / item4 values are visible.
For item 3/4 i get empty textfields.
After submission i don't get any value from #value on my item in $form_state, and rendered page does not keep entered values

If i use #value instead of #default_values, i have textfields with values visible but in validation when i get #value from $form_state i have original values and not entered values.
After submission page is displayed again with old values.

For a textfield not in a table, it is working as i expect.

Thanks for your help.

Ludovic

Comments

r3z0nate’s picture

If you create the row manually, and use references for the fields the elements in the table will work. I'm not sure if you found a different solution but this is one of the many problems with form tables I've found.