我已经使用“use-ajax”类在模式中呈现登录表单。我想在不关闭它的情况下处理同一模式上的验证错误。成功登录时,它会正确重定向,但是当发生错误时,它会关闭模式并重定向到登录页面,即用户/登录并在该页面上显示错误。我尝试使用 ajax 回调通过更改正在工作的表单来显示模态本身的错误。但是,它给了我 drupal ajax 错误。这是我的代码:
$form['#prefix'] = '<div id="modal-form">';
$form['#suffix'] = '</div>';
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$form['actions']['submit']['#ajax'] = array(
'callback' => 'setMessage',
'wrapper' => 'modal-form',
);
==================================================== ========================
function setMessage(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$response = new AjaxResponse();
if ($form_state->hasAnyErrors()) {
$response->addCommand(new ReplaceCommand('#modal-form', $form));
}
else {
$command = new CloseModalDialogCommand('#modal-form', FALSE);
$response->addCommand($command);
}
return $response;
}
上面的代码也给了我会话ID,但由于drupal ajax错误,它不会通过关闭模式重定向成功。
如果我使用非 ajax 即。如果我删除 ajax 回调函数,它会成功,但错误不会显示在模式上。