ajax - Drupal 7——#AJAX 刷新不显示 drupal_set_message 错误

标签 ajax drupal drupal-fapi

问题:

在 Drupal 7 的表单 API 中,当使用#AJAX 刷新字段时,验证错误消息不会显示,直到整个页面被刷新。我看到我刷新的字段在错误状态下突出显示,但用户看不到相关消息,直到为时已晚(他们已经重新加载页面,或转到另一个页面)。

我开始手动处理这个庄园的错误堆栈:Drupal.org -- Filter out specific errors during validation ,但我的表格很复杂,而且完成这​​项任务的时间预算很少。必须有某种方法来刷新堆栈并向用户显示消息,而无需手动处理所有内容。

注意: 我正在使用带有回调的多命令,所以利用它是我的一个选择。

  $commands[] = ajax_command_replace("#my_wrapper", render($form['test']['field_a']));
  $commands[] = ajax_command_replace("#another_wrapper", render($form['test']['field_b']));

  return array('#type' => 'ajax', '#commands' => $commands);

想法?

最佳答案

解决方法:

显然,当您使用多回调方法时,Drupal 不会为您刷新消息。您可以手动执行此操作,如下所示:

  // Remove the old messages div, clearing existing messages.
  $commands[] = ajax_command_remove('#messages');
  // Append a new messages div with our latest errors and messages.
  $commands[] = ajax_command_after('#header-region', '<div id="messages">' . theme('status_messages') . '</div>');

只需将它添加到您拥有的任何回调命令[]数组中,就可以了。

感谢Drupal.org -- AJAX commands and Drupal Messages为正确的方向!

关于ajax - Drupal 7——#AJAX 刷新不显示 drupal_set_message 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16592117/

相关文章:

JQuery .click() 不适用于使用 .after() 添加到 DOM 的 HTML?

php - 尝试从数组中获取值,drupal

html - 无法从 Drupal 样式表内联获取 div

php - Drupal 表单验证对我不起作用

php - 从模块在 drupal 中形成布局

php - 提交后显示表单数据

javascript - ASP.NET 如何知道请求是否为 AJAX?

javascript - 如何阻止恶意代码欺骗 "Origin" header 以利用 CORS?

javascript - Rails,使用 AJAX 创建对象

drupal - 为管理员禁用 Drupal 的 CSS/JS 聚合