János Frajka
János Frajka

Reputation: 1

Edit Drupal 11 Ajax confirm_form formsubmit() not run

I am creating a form that uses an ajax modal confirm form. The confirm form is displayed, but pressing the "Confirm" button just disappears the panel, but the submitform() never runs. If I call the confirm form separately without ajax OpenModalDialog, it works, formsubmit() runs. How could this be implemented?

original form delete button:


'delete' => [
'#type' => 'button',
'#value' => $this->t('Delete'),
'#ajax' => [
'callback' => [$this, 'openDeleteDialog'],
'event' => 'click',
],
'#attributes' => [
'data-id' => $row->id,
],
],

public function openDeleteDialog(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state->getTriggeringElement();
$id = $triggering_element['#attributes']['data-id'];

$response = new AjaxResponse();
$form_object = \Drupal::formBuilder()->getForm('Drupal\rdw\Form\RdwFizmodDijAttekintesConfirmDeleteForm', $id);

$response->addCommand(new OpenModalDialogCommand(
$this->t('Delete Confirmation'),
$form_object,
['width' => '400']
));

return $response;
}

Confirm form:

<?php

namespace Drupal\rdw\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

class RdwFizmodDijAttekintesConfirmDeleteForm extends ConfirmFormBase {

protected $id;

public function buildForm(array $form, FormStateInterface $form_state, $id = NULL) {
$this->id = $id;
return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}

public function getFormId() {
return 'rdw_fizmod_dij_attekintes_confirm_delete';
}

public function getQuestion() {
return $this->t('Are you sure you want to delete row %id?', ['%id' => $this->id]);
}

public function getCancelUrl() {
return new Url('<current>');
}

public function submitForm(array &$form, FormStateInterface $form_state) {
\Drupal::database()->delete('rdw_fizmod_dij')
->condition('id', $this->id)
->execute();

\Drupal::messenger()->addMessage($this->t('Row %id has been deleted.', ['%id' => $this->id]));

$form_state->setRedirect('<current>');
}
}

Ajax modal form submit button now working.

Upvotes: 0

Views: 17

Answers (0)

Related Questions