Custom validate exposed filter on a view I have a view with a single exposed taxonomy field that requires the user to click apply before any results are shown.

The options in the list appear like this


Please Select (All label overridden with form alter, also the default option)
option 1
option 2


if the user clicks apply when please select field is highlighted I would like to show an error message rather than all of the results I have tried the following.

Form:

$form['#validate'][] = 'MY_MODULE_claims_validate';


Function:

function MY_MODULE_forms_claims_validate(&amp;$form, &amp;$form_state) {
  if($form_state['values']['user_category'] == 'All') {
    // $form_state['values']['user_category'] = 0;
    form_set_error('user_category', 'please enter a value');
  }
}


It does set a form error message but also throws the follwoing warning, it also does this on the first page load which I don't want it to do either.

Notice: Undefined index: nid in views_handler_field_term_node_tid->pre_render()

It throws this error the first time the page is loaded which is a
Another approach is to use hook_form_FORM_ID_alter() like other answers suggest, but to put custom validation before the default validation callback of views. By this way you can stop to step over to the second validation callback if validation fails, otherwise validation continues to process.. Example:

function MY_THEME_OR_MODULE_form_views_exposed_form_alter(&amp;$form, &amp;$form_state) {
  if ($form['#id'] == 'views-exposed-form-MY_VIEWS_NAME_MY_VIEWS_DISPLAY') {
    array_unshift($form['#validate'], 'my_custom_validation_callback');
  }
}

function my_custom_validation_callback($form, &amp;$form_state) {
  if(/* CONDITION */) {
    form_set_error('user_category', 'please enter a value');
  }
}