Has anyone successfully created a ctools wizard form that moves between steps with ajax? If so, can documentation be provided on the steps needed to implement?

Issue:
When attempting to move between steps on a wizard form with ajax enabled, a 500 error occurs when hitting the "next" button.
Drupal\Core\Form\FormAjaxException: in Drupal\Core\Form\FormBuilder->buildForm() (line 334 of /var/www/html/core/lib/Drupal/Core/Form/FormBuilder.php).

To reproduce:
When i was attempting to make this work, I simply copied the ctools_wizard_test module into my modules directory and modified the routing.yml. I added js: 'ajax' to the ctools.wizard.test.step and ctools.wizard.test routes after finding line 77 of WizardFormController.php that seemed to indicate adding that info to the routing would cause the form to work via ajax.

ctools.wizard.test:
  path: '/ctools/wizard'
  defaults:
    _wizard: '\Drupal\ctools_wizard_test\Wizard\WizardTest'
    _title: 'Wizard Test'
    tempstore_id: 'ctools.wizard.test'
    machine_name: 'WizardTest'
    js: 'ajax'
  requirements:
    _access: 'TRUE'
ctools.wizard.test.step:
  path: '/ctools/wizard/{step}'
  defaults:
    _wizard: '\Drupal\ctools_wizard_test\Wizard\WizardTest'
    _title: 'Wizard Test'
    tempstore_id: 'ctools.wizard.test'
    machine_name: 'WizardTest'
    js: 'ajax'
  requirements:
    _access: 'TRUE'

I was intially attempting to get this whole thing to work within a modal so i additionally created a new route and controller that simply outputs a button that opens the ctools.wizard.test route in a modal. This does cause step one of the form to load in a modal, but the same error occurs when attempting to hit "next".

I have tried this with core versions 8.2.7 and 8.3.0.

At this point, im not sure if there is a bug or if I am missing something required to make this work correctly. Any assistance would be greatly appreciated.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

morseCode created an issue. See original summary.

csechols’s picture

Here is a patch that fixes this issue from my testing. Inside FormWizardBase.php, the submitForm() and previous() methods were lacking the logic for switching steps when ajax is being used.

Lukas von Blarer’s picture

Status: Active » Needs work

The patch didn't fix my the error in my case.

jmoreira’s picture

Patch on #2 works for me.

shubham.prakash’s picture

Status: Needs work » Needs review
gonssal’s picture

I can confirm that the patch works and still applies.

There's a new function getPrevOp() that is not used and could be removed, but there's also getNextOp() so it can be left there.

In my case I didn't want to use modal dialogs so I'm replacing the entire forms instead.

thalles’s picture

Status: Needs review » Reviewed & tested by the community

The patch is applicable and apparently already was tested

joelpittet’s picture

Category: Support request » Bug report
Status: Reviewed & tested by the community » Needs work

getPrevOp()

should be removed if it's not used.

anagomes’s picture

Assigned: Unassigned » anagomes
anagomes’s picture

Assigned: anagomes » Unassigned
Status: Needs work » Needs review
FileSize
1.61 KB
478 bytes
paulocs’s picture

Status: Needs review » Reviewed & tested by the community

Just tested the patch #10 locally and it is working as expected...
I was able to move between the pages via ajax. The code looks good as well.

  • japerry committed 6b459db on 8.x-3.x
    Issue #2867422 by anagomes, csechols, morseCode: 500 error when...
japerry’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.