AJAX Wizard example cannot be extended with a managed_file

Created on 30 June 2022, over 2 years ago
Updated 19 February 2023, over 1 year ago

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)

🐛 Bug report
Status

Needs work

Version

4.0

Component

AJAX Example

Created by

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

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

Production build 0.71.5 2024