If I have a form with element names as array members, for example:

  foreach($mixComponents as $index => &$mixComponent) {
    $form["isCement[$index]"] = array('#name' => "isCement[$index]", ...)
  }

In the form state I get:

Array ( 
  ... 
  [values] => Array ( 
    [isCement[1]] => 1 [isCement[2]] => 1 [isCement[3]] => 0 ... 
    ...
    ) 
    [clicked_button] => Array ( 
    ...
    [#post] => Array ( 
        [isCement] => Array ( [1] => 1 [2] => 1 [3] => 0 ... ) 
    ...
    ) 
    ...
) 

In some cases, it would be convenient if the 'values' array were converted like PHP does it for posted arrays.

this:
[isCement] => Array ( [1] => 1 [2] => 1 [3] => 0 ... )

instead of this:
[isCement[1]] => 1 [isCement[2]] => 1 [isCement[3]] => 0 ...

Comments

heine’s picture

Why not use:

$form['isCement'] => array('#tree' => TRUE);
$form["isCement"][$index] = ...
cquezel’s picture

Thank you for your quick and very helpful reply. This is exacly what I was trying to accomplish.

I now have another tiny problem that is linked with this strategy. If I undersand correctly, I should not specify the name explicitly so that Drupal generates the correct name. Unfortunately, if I have an array of submit buttons, by not explicitly specifying the name, I cannot distinguish which button was submitted in the form because the name does not appear in the clicked_button info.

[clicked_button] => Array ( 
	??? no #name here ???
	[#type] => image_button 
	[#post] => Array ( 
                ...
		[delete] => Array ( [5] => 7 ) 
                ...

The easy solution is to explicitly set the '#name' property but this kind of makes things a bit redundant (also #name is flagged as internal (a pretty bad name choice for an internal variable considering that html forms use this)).

    $form['delete']['#tree'] = TRUE;
    $form['delete'][$index] = array ('#name' => 'delete['.$index.']', ...

The perfect solution (for me) would be for the name to be set automatically in the clicked_button and to return a parsed name as an array when the '#tree' property is specified. Like

[clicked_button] => Array ( 
	[#name] => Array([0] = delete [1] = 5) // Notice the parsed branch of the tree structure here (could be a string as before in absence of a tree).
	[#type] => image_button 
	[#post] => Array ( 
                ...
		[delete] => Array ( [5] => 7 ) 
                ...

This way the programmer would not have to parse the name in handlers.

pasqualle’s picture

Category: feature » support
Status: Active » Closed (fixed)

if you have an array of submit buttons, like delete buttons in table, you should use the #value parameter

$form['remove'] = array(
  '#type' => 'submit',
  '#value' => 'remove_' . $delta,
  ...

if you have 2 buttons like save and delete then use 2 form elements

$form['save'] = array(
 ...
$form['delete'] = array(
 ...