问题:
在 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>');
只需将它添加到您拥有的任何回调命令[]数组中,就可以了。
关于ajax - Drupal 7——#AJAX 刷新不显示 drupal_set_message 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16592117/