我使用了'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错误,它不会通过关闭code来重定向成功。
如果我使用非ajax ie。如果我删除ajax回调函数,它将成功工作,但错误不会以模式显示。
发布于 2020-03-11 18:01:58
首先,检查是否在模块文件中使用hook_login添加了与重定向相关的更改。您可以删除与重定向相关的更改,并在回调函数中处理重定向。
function setMessage(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$response = new AjaxResponse();
$current= \Drupal::currentUser();
if ($form_state->hasAnyErrors()) {
$response->addCommand(new ReplaceCommand('#modal-form', $form));
}
else if (!$current->id()) {
$response->addCommand(new ReplaceCommand('#modal-form', $form));
}
else {
$command = new RedirectCommand(' ');
return $response->addCommand($command);
}
return $response;
} 如果成功,它将关闭模式并正确重定向。如果您发现任何错误或如果您没有登录,它将停留在模式表单。
https://stackoverflow.com/questions/55914362
复制相似问题