【问题标题】:Drupal 7 -- #AJAX Refresh not displaying drupal_set_message errorsDrupal 7 -- #AJAX Refresh 不显示 drupal_set_message 错误
【发布时间】:2013-05-16 15:52:16
【问题描述】:

问题:

在 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);

想法?

【问题讨论】:

    标签: ajax drupal drupal-fapi


    【解决方案1】:

    解决方案:

    显然,当您使用多回调方法时,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的正确方向!

    【讨论】:

      【解决方案2】:

      感谢阿托莫克斯!对于那些还没有使用多命令的人来说,这只是一个更完整的例子:

       $form['ld']['letterdrop_allocations_submit'] = array(
           '#type' => 'submit',
           '#value' => 'Letterdrop allocations update',
           //        '#name' => 'Letterdrop allocations update',
           '#name' => 'agc_letterdrop_submit',
           '#ajax' => array(
             'callback' => 'agc_ems_form_letterdrop_submit_ajax',
             ),
           );
      
       ...
      
      function agc_ems_form_letterdrop_submit_ajax($form, &$form_state) { 
        drupal_set_message('here i am world', 'ok'); 
        $commands = array(); 
        $commands[] = ajax_command_replace('#messages', 
            '<div id="messages">' . theme('status_messages') . '</div>'); 
        $commands[] = ajax_command_alert('submitted via handler'); 
        return array( 
            '#type' => 'ajax', 
            '#commands' => $commands); 
      } 
      

      也更简洁一些,并消除了消息位于标题区域下方的假设:

      $commands[] = ajax_command_replace('#messages', '<div id="messages">' . theme('status_messages') . '</div>');
      

      【讨论】:

      • 很棒的,更符合上下文的例子。谢谢,埃里希!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多