Closed (fixed)
Project:
Drupal core
Version:
7.x-dev
Component:
forms system
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
21 Jan 2009 at 21:14 UTC
Updated:
25 Oct 2013 at 07:59 UTC
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
Comment #1
heine commentedWhy not use:
Comment #2
cquezel commentedThank 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.
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)).
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
This way the programmer would not have to parse the name in handlers.
Comment #3
pasqualleif you have an array of submit buttons, like delete buttons in table, you should use the #value parameter
if you have 2 buttons like save and delete then use 2 form elements