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 &$form, FormStateInterface $form_state) {
drupal_get_messages(); // Hide the status messages
$form_state->setRebuild(TRUE);
if ($errors = $form_state->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->addCommand(new OpenModalDialogCommand($title, $text));
return $response;
}
}
messages
element to the start of the form.