You are here

function search_api_location_page_edit_validate in Search API Location 7.2

Additional validation function for the altered search page add / edit form.

2 string references to 'search_api_location_page_edit_validate'
search_api_location_page_form_search_api_page_admin_add_alter in search_api_location_page/search_api_location_page.module
Implements hook_form_FORM_ID_alter().
search_api_location_page_form_search_api_page_admin_edit_alter in search_api_location_page/search_api_location_page.module
Implements hook_form_FORM_ID_alter().

File

search_api_location_page/search_api_location_page.module, line 130

Code

function search_api_location_page_edit_validate(array $form, array &$form_state) {
  $form_values =& $form_state['values'];

  // Little trick to work with both the add and the edit form.
  if (!isset($form_values['search_api_location'])) {
    if (isset($form_values['options'])) {
      unset($form_values);
      $form_values =& $form_state['values']['options'];
      $form = $form['options'];
    }
    else {
      return;
    }
  }

  // Loop over all location fields.
  foreach ($form_values['search_api_location'] as $key => $values) {
    if (empty($values['enabled'])) {
      continue;
    }
    $sub_form = $form['search_api_location'][$key];
    switch ($values['radius_type']) {
      case 'fixed':
        $value = $values['radius_value'];
        if ($value != '' && !is_numeric($value) || (double) $value < 0) {
          form_error($sub_form['radius_value'], t('Maximum distance must be a non-negative number.'));
        }
        break;
      case 'select':
        $options = array();
        $lines = array_filter(array_map('trim', explode("\n", $values['radius_options'])));
        foreach ($lines as $line) {
          list($range, $label) = explode(' ', $line, 2);
          if ($range == '-') {
            $range = NULL;
          }
          elseif (is_numeric($range)) {
            $range = (double) $range;
          }
          else {
            continue;
          }
          $options[$range] = trim($label);
        }
        if (empty($options[$range])) {
          form_error($sub_form['radius_options'], t('Specify at least one distance option.'));
        }
        $form_values['search_api_location'][$key]['radius_options'] = $options;
        break;
    }
  }
}