-
Notifications
You must be signed in to change notification settings - Fork 40
Description
Description of the need
Sometimes when creating a form, it's useful to group fields together in an invisible wrapper using #type = container. This happens commonly when using #ajax to update a section of a form.
The problem with nesting form elements however is that it affects the values within $form_state['values']. Usually the structure of the form and the structure of $form_state['values'] are directly mapped to each other (with the #tree property controlling the nesting behavior.
The $form_state['values'] mapping directly to the form causes problems in a lot of situations where the form values are saved automatically for you, examples include:
hook_field_widget_settings_form()hook_field_settings_formsystem_settings_form()- Probably many others
Proposed solution
Introduce a companion Form API property: #tree_skip
This new property would make it so that elements nested under a container would not have their values automatically nested within $form_state['values'].
Without #tree_skip:
$form['#tree'] = TRUE;
$form['container'] = array(
'#type' => 'container',
);
$form['container']['foo'] = array(
'#type' => 'textfield',
'#title' => t('Example'),
'#default_value' = 'hello',
);
// Results in the following:
$form_state['values']['container']['foo']; // hello
With #tree_skip:
$form['#tree'] = TRUE;
$form['container'] = array(
'#type' => 'container',
'#tree_skip' => TRUE,
);
$form['container']['foo'] = array(
'#type' => 'textfield',
'#title' => t('Example'),
'#default_value' = 'hello',
);
// Results in the following:
$form_state['values']['foo']; // hello
This would allow the creation of visual-only wrappers such as fieldsets, details, and containers without affecting the submitted values, making creating forms much easier in some situations.
Alternatives that have been considered
In situations where this occurs currently, developers have used the #parents property to override the automatic form values mapping. There is a good blog article on using #parents in Drupal here, which the same concept applies for Backdrop.
Draft of feature description for Press Release (1 paragraph at most)
Backdrop now includes a new Form API property #tree_skip that allows developers to create visual-only wrappers within forms. This makes writing forms that utilize AJAX callbacks or nest elements for display purposes much easier.