6

I have a form that allows users to upload a CSV file. Upon submit, I would like to show a confirmation form ("Are you sure you want to import this file?").

However, the Drupal 8 examples for ConfirmFormBase seem to only focus on building the confirmation form without actually showing how one gets to that form from another form.

Can someone provide an example of a regular form that then works in conjunction with a confirmation form?

2 Answers 2

4

As you've already discovered, ConfirmFormBase just provides some buttons, handling for messages, and submit/cancel logic. On its own, it's not designed to be used as the sort of multistep form you're looking for.

Getting that functionality is pretty easy though; you could use the logic in Drupal\config\Form\ConfigSingleImportForm as a rough example. Basically you just handle the multistep part of it yourself. For example:

protected $step = 1;

public function buildForm(array $form, FormStateInterface $form_state) {
  // When this is the confirmation step fall through to the confirmation form.
  if ($this->step === 2) {
    return parent::buildForm($form, $form_state);
  }

  // Build and return your upload form as normal, but don't call the parent method.
}

public function validateForm(array &$form, FormStateInterface $form_state) {
  // The confirmation step needs no additional validation.
  if ($this->step === 2) {
    return;
  }

  // Perform any additional validation 
}

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($this->step === 1) {
    $form_state->setRebuild();
    $this->step = 2;
    return;
  }

  // Extract rows from CSV and set batch to import here
}
2
  • 1
    I tried implementing this, but I ran into problem in the submitForm function in step 2, $form_state did not have the values saved anymore from the step 1 form. I fixed this by adding this line in submitForm after line $this->step = 2;: $this->data = $form_state->getValues(); That way, in step 2 you can use $this->data to reference all the values from step 1.
    – Risse
    Commented Jan 19, 2019 at 8:47
  • @Clive This answer pertains to Config... but the OP is about Confirm... The answer may need some editing/corrections, right? Commented Nov 23, 2019 at 22:25
0

I've come to the disappointing conclusion that it's simply not feasible to add confirmation to an existing form that does not already extend ConfirmFormBase. The accepted answer is only useful as an example if you are creating a completely new form. In order to add this to an existing form you would first need to override the existing form class with a new class that you control. This is possible. However, in order to avoid duplicating all of the code from the existing class, you would want to extend that class and only modify the necessary methods while calling the parent methods inside your modified methods when possible. But then of course your new class does not extend ConfirmFormBase, so you are right back where you started.

If you would like to see this issue resolved in core, please see this core issue: https://www.drupal.org/project/drupal/issues/2883755

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.