Show modal modal message on form success I have a custom form that submits via AJAX. On form error, i simply return the whole form on my callback function, and the HTML is replaced, but i want it to open a modal dialog on success. What is the best way to do this? This is what i'm currently doing (i don't like this solution because i can't clear the form values, among other things):

    public function ajaxCallback(array &amp;$form, FormStateInterface $form_state) {

        drupal_get_messages(); // Hide the status messages

        $form_state-&gt;setRebuild(TRUE);

        if ($errors = $form_state-&gt;getErrors()) {

          return $form;
        }
        else {

          $response = new AjaxResponse();

          $title = 'Consectetur adipiscing elit';
          $text  = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';

          $response-&gt;addCommand(new OpenModalDialogCommand($title, $text));

          return $response;
        }
      }
Here is what i came up with (many thanks to @4k4):

    public function ajaxCallback(array &amp;$form, FormStateInterface $form_state) {

        drupal_get_messages(); // Hide messages

        $form_state-&gt;setRebuild(TRUE);

        if ($errors = $form_state-&gt;getErrors()) {

           return $form; // Simply Return the form
        }
        else {

         $response = new AjaxResponse();

         $title = 'Consectetur adipiscing elit';
         $text  = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';

        $response-&gt;addCommand(new OpenModalDialogCommand($title, $text));

        // Or add the $form as parameter to an HTML command.
        $response-&gt;addCommand(new ReplaceCommand('#my-form-wrapper', $form)); 

        // Do notice i added a wrapper to my form in the buildForm() method:
        // $form['#prefix'] = '&lt;div id="my-form-wrapper"&gt;';
        // $form['#suffix'] = '&lt;/div&gt;';

         return $response;
    }