1

I am trying to add validation errors to exposed views filters (drop down lists) in block region. However, my validation error runs on the first page load before I hit apply button.

Also the validation doesn't run on hitting the apply button as the validation function has already been run on first page upload. I have also tried 'views->advance->views exposed forms->input required, but no luck.

I think its a known bug in exposed views filters.I have been trying to sort this issue for last three days and went thorough several discussion forums. I will be grateful if anyone can share a patch or guideline through which I can have my validation function run only when apply/submit button is clicked. Here is my code:

/**
 * Implements hook_form_alter().
 */
function custom_forms_form_alter (&$form, &$form_state, $form_id) {
    switch ($form_id) {
        case 'views_exposed_form':{
        $form['#validate'][] = 'custom_forms_views_exposed_form_validate';
        break;
        }
    }
}

/**
 * Validate views exposed filters in the block region.
 */
function custom_forms_views_exposed_form_validate ($form, &$form_state){
    $value = $form_state['values']['field_number_of_bedrooms_value'];
    if ($value == "Select any"){
        form_set_error ('field_number_of_bedrooms_value', t('You must select valid numbers of bedrooms.'));
    }
}

Note: The value 'Select any' is a node field value with no results. I created it to be used as a default value that displays no results on first page load. I even changed the default value to '-Any-' from views interface, but no success. All the fields are set to 'Required' and the exposed form also has 'Reset' button

Your Answer

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

Browse other questions tagged or ask your own question.