Score:0

How disable submit button after call OpenModalDialogCommand in Ajax callback?

us flag

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;
}
Score:0
cn flag

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', []));
us flag
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));
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.