0

我有一个带有公开过滤器表单的视图块(视图 3 / Drupal 7),并且启用了 ajax 模式。它工作正常。我在 hook_form_alter() 中添加了一个验证函数。它也有效,但 form_set_error 消息仅在页面刷新时显示。如何在不重新加载页面的情况下设置消息?

function hook_form_alter(&$form, &$form_state, $form_id) {
  if($form['#id'] === 'id_from_views') {
   array_unshift($form['#validate'], '_custom_form_validate');
  }
}

function _custom_form_validate($form, &$form_state) {
 if(!empty($form_state['values']['field'])) {
   form_set_error('field', t('Custom error message.'));
  }
}
4

1 回答 1

0

我有一个类似的问题。答案是在 hook_form_alter 中使用 ajax 回调。

 function hook_form_alter(&$form, &$form_state, $form_id) {
      if($form['#id'] === 'id_from_views') {
       $form['submit']['#ajax'] = array('callback' => '_custom_form_validate');
      }
    }
于 2014-05-23T09:43:41.020 回答