3

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(&$form, &$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

  • Do you want to perform the validation or get rid off the warnings? Currently your question seems un clear. – Mathankumar Jan 21 '14 at 10:15
  • The code above works I just need to work out why it's throwing that warning and also prevent the validation firing on the first page load. – Daniel Harper Jan 21 '14 at 10:39
  • I've tried using the same technique with several views and I always get the same error message. Notice: Undefined index: nid in views_handler_field_term_node_tid->pre_render() – Daniel Harper Jan 23 '14 at 15:36
4
+50

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(&$form, &$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, &$form_state) {
  if(/* CONDITION */) {
    form_set_error('user_category', 'please enter a value');
  }
}
  • Tried the above and it does validate the field but I still get the error message, it also tries to validate the form on first page load and throws the error and also my custom validate error. – Daniel Harper Jan 23 '14 at 14:51
  • @DanielHarper I think you need to show the views configuration and your form alteration and so on.. Which is related to this functionality... Because I don't think that error in your form alter function or validation callback, it is in somewhere else.. – xurshid29 Jan 24 '14 at 7:19
  • @DanielHarper The problem is in your views configuration.. I think you've added somekind of node field and when you execute the views it tries to load that field but you are not allowing it to be chosen, that's why you are getting this error.. Take a look at this page: whatwherewhenroslindale.com/… When you open it you get an error. And validation shows that End date must be chosen.. I think you need to provide default value to that field somehow.. – xurshid29 Jan 24 '14 at 7:25
  • You're right I have a taxonomy field which is not rendered but used to group the view for quicktabs, is there a way to manually set these values in the validate function or do I need to use a different views hook? – Daniel Harper Jan 24 '14 at 10:31
  • @DanielHarper I don't have a working project where I can test it and I don't know your view's configuration... With hook_form_FORM_ID_alter can set default values to views. Here is a simple tutorial (I don't this can help you:)): views-help.doc.logrus.com/help/views/alter-exposed-filter and here: venutip.com/content/setting-default-value-views-exposed-filters – xurshid29 Jan 24 '14 at 10:54
2

If you are using Drupal 7, I'd leverage the Form API's #states functionality and only allow the (APPLY) to appear when you've selected something in your exposed filter.

For a quick example, I just threw together a View that exposes the content type...

enter image description here

...and then a quick form_alter() along these lines...

function MODULE_OR_THEME_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id=='views_exposed_form') { // obviously you'll probably need to be more specific :)
    // drupal_set_message('<pre>' . print_r($form, TRUE) . '</pre>'); // uncomment to see what you have to work with
    $form['type']['#options']['All']='Please Select';
    $form['type']['#description']=t('You must select a type before continuing.');
    $form['submit']['#states']=array(
      'enabled' =>  array(':input[name="type"]' => array('!value' => 'All')), // enable this when we aren't selecting "All" for our type
      'visible' =>  array(':input[name="type"]' => array('!value' => 'All')), // show it when we aren't selecting "All" as well
      // 'disabled' => array(':input[name="type"]' => array('value'  => 'All')), // you don't really need this one or the next one but include them
      // 'hidden' =>   array(':input[name="type"]' => array('value'  => 'All')), // for completeness or if your site does need them (mine didn't)
    );
  }

}

which, together, produces on the first page load:

enter image description here

and when a type has been selected:

enter image description here

thus allowing your users to look at the View.

Obviously, for your View, you'll have to change the 'type' to your taxonomy field as well as the name= to the correct name, too, but the broad strokes are here.

  • Thanks for the answer @jimajamma although this would have worked I wanted to leave the button in tact as I thought it would be less confusing for users. – Daniel Harper Jan 24 '14 at 9:37
0

You can use hook_form_alter() to attach a custom #validate callback:

function my_module_form_alter(&$form, &$form_state, $form_id) {
  drupal_set_message($form_id);
  if ($form_id == 'my_exposed_form') {
    $form['#validate'][] = 'my_custom_validate';
  }
}

function my_custom_validate($form, &$form_state) {
  // Do custom validation here...
  // If there was a problem...
  form_set_error('my_taxonomy_exposed_field', t('Something bad happened!'));
}

Alternatively, you may want to overwrite the #validate handler, rather than appending your validate handler to it, if the default handler is causing problems.

Also, on your exposed filter, check out the different 'operator' settings as a way to help prevent all results from being displayed if nothing is selected.

  • I have updated my question to include a solution as above but it doesn't quite work as I needs, also explained above. – Daniel Harper Jan 21 '14 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.