diff --git a/CRM/Contact/Form/Search/Custom.php b/CRM/Contact/Form/Search/Custom.php index e82d7ad71f2c..4af599800f72 100644 --- a/CRM/Contact/Form/Search/Custom.php +++ b/CRM/Contact/Form/Search/Custom.php @@ -83,6 +83,8 @@ public function preProcess() { // 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 @@ public function preProcess() { } } + /** + * Add local and global form rules. + */ + public function addRules() { + $this->addFormRule(array($this->_customClass, 'formRule')); + } + /** * Set the default values of various form elements. * diff --git a/CRM/Contact/Form/Search/Custom/Base.php b/CRM/Contact/Form/Search/Custom/Base.php index 19c1a91a4df4..74ed7650573a 100644 --- a/CRM/Contact/Form/Search/Custom/Base.php +++ b/CRM/Contact/Form/Search/Custom/Base.php @@ -257,4 +257,18 @@ public function setTitle($title) { } } + /** + * 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 []; + } + } diff --git a/CRM/Contact/Form/Search/Custom/Proximity.php b/CRM/Contact/Form/Search/Custom/Proximity.php index 5a86cebdd930..9d90c55a8a82 100644 --- a/CRM/Contact/Form/Search/Custom/Proximity.php +++ b/CRM/Contact/Form/Search/Custom/Proximity.php @@ -55,24 +55,7 @@ public function __construct(&$formValues) { 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 @@ public function buildACLClause($tableAlias = 'contact') { 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); + } + } diff --git a/CRM/Report/Form/Contribute/Repeat.php b/CRM/Report/Form/Contribute/Repeat.php index 1d84e5bdbab0..900825bdf0ff 100644 --- a/CRM/Report/Form/Contribute/Repeat.php +++ b/CRM/Report/Form/Contribute/Repeat.php @@ -513,9 +513,9 @@ public function where() { } /** - * @param $fields - * @param $files - * @param $self + * @param array $fields + * @param array $files + * @param CRM_Core_Form $self * * @return array */ diff --git a/CRM/Report/Form/Contribute/SoftCredit.php b/CRM/Report/Form/Contribute/SoftCredit.php index 0ad8445917a2..e9ef1772b786 100644 --- a/CRM/Report/Form/Contribute/SoftCredit.php +++ b/CRM/Report/Form/Contribute/SoftCredit.php @@ -417,9 +417,9 @@ public function select() { } /** - * @param $fields - * @param $files - * @param $self + * @param array $fields + * @param array $files + * @param CRM_Core_Form $self * * @return array */