I want to add my custom validation to the views exposed filter form for that I have added below code in hook_form_alter

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

But its being called for form load also, is there any specific way to add custom validation function for views exposed filters form.


To form alter a specific view exposed filter form, you need 2 conditions to check: the form id & the form's '#id' element.

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

  // @NOTE: $form['#id'] will vary based on your your view & display machine names.
  // Here the view machine name is 'view' & display machine name is 'display'.
  // Use Devel's dpm() to inspect or the Views administration UI
  // to find your own form's values.

  if ($form_id == 'views_exposed_form') {
    if ($form['#id'] == 'views-exposed-form-view-display') {
      // @TODO: Add form alter code.

  • I have did the same but form validate is getting called before form submit
    – Sri Harsha
    Apr 16 '16 at 4:26
  • I'm not sure what you're asking then. Validation handlers are supposed to be executed before submission handlers.
    – Shawn Conn
    Apr 16 '16 at 6:12

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.