Problem/Motivation

Adding a managed_file field after step1 of the Wizard multi-step AJAX form example breaks the form. For the sake completeness and usefulness, this example should support all types of fields, including Managed Files.

Steps to reproduce

1. Enable the ajax_example module

2. Edit examples/modules/ajax_example/src/Form/Wizard.php by adding a new managed_file field on step2:

...
case 2:
        $limit_validation_errors = [['step'], ['step1']];
        $form['step1'] = [
          '#type' => 'value',
          '#value' => $form_state->getValue('step1'),
        ];
        $form['step2'] = [
          '#type' => 'fieldset',
          '#title' => $this->t('Step 2: Street address info'),
        ];
        $form['step2']['address'] = [
          '#type' => 'textfield',
          '#title' => $this->t('Your street address'),
          '#default_value' => $form_state->hasValue(['step2', 'address']) ? $form_state->getValue(['step2', 'address']) : '',
          '#required' => TRUE,
        ];
        $form['step2']['test_file'] = [
          '#type' => 'managed_file',
          '#upload_location' => 'public://attachments/',
          '#multiple' => TRUE,
          '#description' => 'This is a description',
          '#upload_validators' => [
            'file_validate_extensions' => [],
            'file_validate_size' => array(25600000)
          ],
          '#default_value' => $form_state->hasValue(["step2", 'test_file']) ? $form_state->getValue(["step2", 'test_file']) : [],
        ];
        break;
...

3. Navigate to your.site/examples/ajax-example/wizard
4. On the second step of the form, try to upload a File.
5. The form breaks (the ManagedFile field is gone)

Comments

tomurillo created an issue. See original summary.

paul121’s picture

Experiencing the same thing for the dependent dropdown example. I think it is a simpler case to demonstrate this issue so I opened a new issue. I've also identified what is causing the issue (managed_file rebuilds the form_state in a process callback) but have not identified a definitive way to solve this.

#3311557: AJAX dependent dropdown can not be extended with a managed_file

jungle’s picture

Version: 3.0.3 » 4.0.x-dev
Status: Active » Needs work
Issue tags: +Needs tests

Thanks for opening this, 4.0.x is the dev branch for ^9.4 || ^10