CRM-21742 use validation rather than error when geocoding not possible
authoreileen <emcnaughton@wikimedia.org>
Wed, 7 Feb 2018 00:45:16 +0000 (13:45 +1300)
committereileen <emcnaughton@wikimedia.org>
Wed, 7 Feb 2018 02:07:51 +0000 (15:07 +1300)
CRM/Contact/Form/Search/Custom.php
CRM/Contact/Form/Search/Custom/Base.php
CRM/Contact/Form/Search/Custom/Proximity.php
CRM/Report/Form/Contribute/Repeat.php
CRM/Report/Form/Contribute/SoftCredit.php

index e82d7ad71f2ca0bbd36aac4311a79868a8ba1994..4af599800f72021e679ee8c0f338ac98ae379b8c 100644 (file)
@@ -83,6 +83,8 @@ class CRM_Contact_Form_Search_Custom extends CRM_Contact_Form_Search {
     // instantiate the new class
     $this->_customClass = new $this->_customSearchClass($this->_formValues);
 
+    $this->addFormRule(array($this->_customClass, 'formRule'), $this);
+
     // CRM-12747
     if (isset($this->_customClass->_permissionedComponent) &&
       !self::isPermissioned($this->_customClass->_permissionedComponent)
@@ -91,6 +93,13 @@ class CRM_Contact_Form_Search_Custom extends CRM_Contact_Form_Search {
     }
   }
 
+  /**
+   * Add local and global form rules.
+   */
+  public function addRules() {
+    $this->addFormRule(array($this->_customClass, 'formRule'));
+  }
+
   /**
    * Set the default values of various form elements.
    *
index 19c1a91a4df426698ee37deb9885cec4b8de5f25..74ed7650573a1d6ee469aeda47f17bcc019619d3 100644 (file)
@@ -257,4 +257,18 @@ class CRM_Contact_Form_Search_Custom_Base {
     }
   }
 
+  /**
+   * Validate form input.
+   *
+   * @param array $fields
+   * @param array $files
+   * @param CRM_Core_Form $self
+   *
+   * @return array
+   *   Input errors from the form.
+   */
+  public function formRule($fields, $files, $self) {
+    return [];
+  }
+
 }
index 5a86cebdd93083ba8de5ce94f28c422d8157274d..9d90c55a8a824edc4ed4a16ff06debe79690bc41 100644 (file)
@@ -55,24 +55,7 @@ class CRM_Contact_Form_Search_Custom_Proximity extends CRM_Contact_Form_Search_C
 
     if (!empty($this->_formValues)) {
       // add the country and state
-      if (!empty($this->_formValues['country_id'])) {
-        $this->_formValues['country'] = CRM_Core_PseudoConstant::country($this->_formValues['country_id']);
-      }
-
-      if (!empty($this->_formValues['state_province_id'])) {
-        $this->_formValues['state_province'] = CRM_Core_PseudoConstant::stateProvince($this->_formValues['state_province_id']);
-      }
-
-      // use the address to get the latitude and longitude
-      CRM_Core_BAO_Address::addGeocoderData($this->_formValues);
-
-      if (!is_numeric(CRM_Utils_Array::value('geo_code_1', $this->_formValues)) ||
-        !is_numeric(CRM_Utils_Array::value('geo_code_2', $this->_formValues)) ||
-        !isset($this->_formValues['distance'])
-      ) {
-        throw new CRM_Core_Exception(ts('Could not geocode input'));
-      }
-
+      self::addGeocodingData($this->_formValues);
       $this->_latitude = $this->_formValues['geo_code_1'];
       $this->_longitude = $this->_formValues['geo_code_2'];
 
@@ -320,4 +303,43 @@ AND cgc.group_id = {$this->_group}
     list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
   }
 
+  /**
+   * Validate form input.
+   *
+   * @param array $fields
+   * @param array $files
+   * @param CRM_Core_Form $self
+   *
+   * @return array
+   *   Input errors from the form.
+   */
+  public function formRule($fields, $files, $self) {
+    $this->addGeocodingData($fields);
+
+    if (!is_numeric(CRM_Utils_Array::value('geo_code_1', $fields)) ||
+      !is_numeric(CRM_Utils_Array::value('geo_code_2', $fields)) ||
+      !isset($fields['distance'])
+    ) {
+      $errorMessage = ts('Could not determine co-ordinates for provided data');
+      return array_fill_keys(['street_address', 'city', 'postal_code', 'country_id', 'state_province_id'], $errorMessage);
+    }
+    return [];
+  }
+
+  /**
+   * Add the geocoding data to the fields supplied.
+   *
+   * @param array $fields
+   */
+  protected function addGeocodingData(&$fields) {
+    if (!empty($fields['country_id'])) {
+      $fields['country'] = CRM_Core_PseudoConstant::country($fields['country_id']);
+    }
+
+    if (!empty($fields['state_province_id'])) {
+      $fields['state_province'] = CRM_Core_PseudoConstant::stateProvince($fields['state_province_id']);
+    }
+    CRM_Core_BAO_Address::addGeocoderData($fields);
+  }
+
 }
index 1d84e5bdbab02675aa5f960c39688cd121510cc2..900825bdf0ff30163e865d02ef43709aa98b6139 100644 (file)
@@ -513,9 +513,9 @@ LEFT JOIN $this->tempTableRepeat2 {$this->_aliases['civicrm_contribution']}2
   }
 
   /**
-   * @param $fields
-   * @param $files
-   * @param $self
+   * @param array $fields
+   * @param array $files
+   * @param CRM_Core_Form $self
    *
    * @return array
    */
index 0ad8445917a259b978c92c0a0e42d833530de127..e9ef1772b786c48b6a570b2c29bd04687fb2ca36 100644 (file)
@@ -417,9 +417,9 @@ class CRM_Report_Form_Contribute_SoftCredit extends CRM_Report_Form {
   }
 
   /**
-   * @param $fields
-   * @param $files
-   * @param $self
+   * @param array $fields
+   * @param array $files
+   * @param CRM_Core_Form $self
    *
    * @return array
    */