Question

I have a views block (Views 3 / Drupal 7) with a exposed filter form and the ajax mode is enabled. It works fine. I added in hook_form_alter() a validation function. It works too, but the form_set_error message shows only on a page refresh. How can I set it up the message without page reload?

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.'));
  }
}
Was it helpful?

Solution

I had a similar problem. The answer is to use ajax callback in the hook_form_alter.

 function hook_form_alter(&$form, &$form_state, $form_id) {
      if($form['#id'] === 'id_from_views') {
       $form['submit']['#ajax'] = array('callback' => '_custom_form_validate');
      }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top