I've been trying to create a location search that functions well for a while. The problem always arises that people insert a comma into the location (San Diego, CA), or something like that, and it breaks the search because the comma isn't stored with the value. I've spent the day trying to solve this and found that apparently I need to use hook_form_alter and validation in a custom module. I've found many examples that I've tried to modify, and none have worked so far. The most promising one I found seemed to be this one. I've modified it as such (only changing validation name and view name):

<?php
/**
* Implementation of hook_form_alter().
*/
function search_helper_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    // Add custom validation form for address search
    if ($form['#parameters']['1']['view']->name == 'address_search') {
      $form['#validate'][] = 'search_modifier';
    }
  }
}

/**
* Makes address search easier.
*/
function search_modifier($form, &$form_state) {
  // Strip "'s" (eg, "St. Sabina's" turns into "St. Sabina")
  $form_state['values']['name'] = preg_replace("/'s$/i", '', $form_state['values']['name']);
}

Although this code supposedly works out of the box, I can't for the life of me figure out how to get it to work for me. Although it only replaces " 's " and not a comma, i figured I can alter it once it's working. Any help would be incredibly appreciated.

Comments

MustangGB’s picture

Status: Active » Closed (outdated)