0

I have a block that has a button, when pressing that button an ajax is triggered that shows a popup with the OpenModalDialogCommand event, after closing the popup I want to deactivate the submit button, does anyone know how to do that?

The form

    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Get cupon'),
      '#prefix' => '<div id="cupon_form_wrapper">',
      '#suffix' => '</div>',
      '#attributes' => $results > 0 ? array('disabled' => 'disabled') : [],
      '#ajax' => array(
        'callback' => '::_modal_form_cupon_ajax_submit',
        'event' => 'click'
      ),
    ); 
function _modal_form_cupon_ajax_submit(array $form, FormStateInterface &$form_state) {
    $response = new AjaxResponse();
    if ($form_state->getErrors()) {
      unset($form['#prefix']);
      unset($form['#suffix']);
      $form['status_messages'] = [
        '#type' => 'status_messages',
        '#weight' => -10,
      ];
      $response->addCommand(new HtmlCommand('#cupon_form_wrapper', $form));
    }
    else {
      $content = 'Lorem ipsum';
      $title = 'Download';
      $response = new AjaxResponse();
      $response->addCommand(
        new OpenModalDialogCommand(
          $title,
          $content,
          array(
            'width'=>'300'
          )
        ),
        $form
      );
    }

    return $response;
}

1 Answer 1

0

Its as simple as adding an extra Command to your AjaxResponse class.

Something like this should work. Make sure you change the css selector (first argument) so it matches your button (or form).

$response->addCommand(new InvokeCommand('.button-selector', 'hide', []));
1
  • Thank you, you gave me a hint of how I should do it, I use this: $form['actions']['submit'] = array( '#type' => 'submit', '#value' => $this->t('Get cupon'), '#prefix' => '<div id="cupon_form_wrapper">', '#suffix' => '</div>', '#attributes' => ['disabled' => 'disabled'], ); $response->addCommand(new HtmlCommand('#cupon_form_wrapper', $form));
    – Corncakes
    Commented Sep 8, 2021 at 20:38

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.