Score:0

Set form values based on ajax callback

us flag

I am creating a complexed multistep form based on data from an Excel document. The user can save parts of the form using AJAX save buttons and updated data from the Excel document are displayed in the form. I am fetching a lot of data from Excel - some are for inputs and some are read-only which are not put into FormState.

The issue is that I need to populate the form with data based on the API callback but as the form is not rebuild after the API call to Excel, the updated data from Excel is not put into the form until the next AJAX call. So the data is always one step behind.

I've tried to simplify the architecture and code:

Form

class Form [..] {
  public Service $service;

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['part_one'] = [
      '#attributes' => [
        'id' => ['part_one'],
      ],
      'input' => [
        '#type' => 'number',
        '#value' => $form_state->getUserInput()['input'] ?? $this->service->model->input,
      ],
      'value' => [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#value' => $this->service->model->value,
      ],
      'save' => [
        '#type' => 'button',
        '#value' => 'Save',
        '#ajax' => [
          'callback' => [$this, 'save'],
          'wrapper' => 'part_one',
        ],
      ],
    ];

    return $form;
  }

  public function save(array &$form, FormStateInterface $formState) {
    $value = $formState->getUserInput()['input'];
    $this->service->save($value);
    return $form['part_one'];
  }
}

Service

class Service {
  public Model $model;
  public function save($value) {
    $httpExcel->save($value);
    $modelValues = $httpExcel->get();
    $model = new Model($modelValues);
  }
}

Model

class Model {
  public int $input;
  public int $value;
}
id flag
The callback returns $form without changing it.
Jaypan avatar
de flag
I wrote up a tutorial on how to create multistep forms, that manage an entity (such as your (Model). It's written for Drupal 7 (IIRC), but the concepts haven't changed, and I still use those concepts to build multistep forms in Drupal 8, 9, and 10. https://www.jaypan.com/tutorial/advanced-ajax-enabled-multi-step-forms-creating-and-altering-drupal-entities-custom-forms. You will want to store your entity in the form state, and save it in the submit handler.
NicklasF avatar
us flag
The problem is that I only know how the model is looking _after_ the ajax callback as the callback is updating the model. So how would I know how to build the form before the ajax then?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.