SubformState should complain when the subform doesn't have #tree = TRUE

Created on 10 May 2019, over 5 years ago
Updated 11 September 2024, 5 months ago

Problem/Motivation

SubformState doesn't work properly if the subform doesn't have #tree = TRUE. That's because without that, the subform's form values are the top of the values array, and SubformState::getValues() won't find them.

So if you do something like this:

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $subform_state = SubformState::createForSubform($form['plugin_form'], $form, $form_state);
    $plugin->submitConfigurationForm($form['plugin_form'], $subform_state);

then the plugin's submitConfigurationForm() won't find the values it expects.

Proposed resolution

Given this is a requirement for SubformState to work properly, I think it should throw an exception in createForSubform() if $form is non-empty and doesn't have #tree set.

It needs to allow an empty array, because in buildForm() you typically do this:

      $form['plugin_form'] = [];
      $subform_state = SubformState::createForSubform($form['plugin_form'], $form, $form_state);
      $form['plugin_form'] = $action->buildConfigurationForm($form['plugin_form'], $subform_state);
      $form['plugin_form']['#tree'] = TRUE;

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Formย  โ†’

Last updated 1 day ago

Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ท๐Ÿ‡ดRomania bbu23

    Well, thank you. You just saved me some time trying to figure out why one of mine didn't work :) After adding #tree I can finally see the values.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

    Cool!

    Tagging as Novice, maybe someone will find time to fix this!

  • ๐Ÿ‡ท๐Ÿ‡ดRomania bbu23

    and partially unrelated, file_save_upload will not work with a subform. Apparently the $form_field_name expects the container name instead of the file field name.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Soham Sengupta

    Soham Sengupta โ†’ made their first commit to this issueโ€™s fork.

  • Status changed to Needs review 7 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Soham Sengupta

    I have added the fix, now the method createForSubform() throws an exception if the #tree property is not set, for non empty forms.
    Moving the issue to Needs Review state.
    Thank you.

  • Pipeline finished with Failed
    7 months ago
    Total: 495s
    #212951
  • Status changed to Needs work 7 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Will need test coverage

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

    Looks like we already have inadvertent test coverage! :D

    All of SubformStateTest is failing because the form arrays aren't setting #tree. So those need to be fixed, and there should be one extra test added there which doesn't set #tree and uses expectException().

  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

    Fixed the unit tests.

    I suspect most of the functional test failures are caused by the one form which I've fixed. Let's see!

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Can issue summary be updated to the standard template, to mention the proposed solution is to throw an exception.

    Then can mark.

    Do wonder if this will break any contrib code and have to do the BC dance.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

    Without #tree, your subform is broken. I don't know how config field forms were managing not to be!

  • Status changed to RTBC 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    That's fair, then throwing exception makes sense.

  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone

    If #tree is required then this needs to be documented, and I can not find anything about that. Can we have a followup for that?

  • Status changed to Needs work 5 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    Without #tree, your subform is broken. I don't know how config field forms were managing not to be!

    But they are. So this potentially will trigger exceptions against currently working code. Because it did against core. I don't think we should do that. Perhaps we can start with a triggering a warning and then move to an exception in the next major version.

Production build 0.71.5 2024