From 2307be0897da1b4090cbb2783a239aebd30fc047 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 4 Mar 2019 13:47:35 +1300 Subject: [PATCH] Add form rule for chronological ordeR --- CRM/Core/Form/Search.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/CRM/Core/Form/Search.php b/CRM/Core/Form/Search.php index f793ae6f3a..c0defa2ac5 100644 --- a/CRM/Core/Form/Search.php +++ b/CRM/Core/Form/Search.php @@ -151,6 +151,7 @@ class CRM_Core_Form_Search extends CRM_Core_Form { * than existing ad hoc handling. */ public function addFormFieldsFromMetadata() { + $this->addFormRule(['CRM_Core_Form_Search', 'formRule'], $this); $this->_action = CRM_Core_Action::ADVANCED; foreach ($this->getSearchFieldMetadata() as $entity => $fields) { foreach ($fields as $fieldName => $fieldSpec) { @@ -164,6 +165,31 @@ class CRM_Core_Form_Search extends CRM_Core_Form { } } + /** + * Global validation rules for the form. + * + * @param array $fields + * Posted values of the form. + * + * @return array + * list of errors to be posted back to the form + */ + public static function formRule($fields, $files, $form) { + $errors = []; + foreach ($form->getSearchFieldMetadata() as $entity => $spec) { + foreach ($spec as $fieldName => $fieldSpec) { + if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || $fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) { + if (isset($fields[$fieldName . '_high']) && isset($fields[$fieldName . '_low']) && empty($fields[$fieldName . '_relative'])) { + if (strtotime($fields[$fieldName . '_low']) > strtotime($fields[$fieldName . '_high'])) { + $errors[$fieldName . '_low'] = ts('%1: Please check that your date range is in correct chronological order.', [1 => $fieldSpec['title']]); + } + } + } + } + } + return $errors; + } + /** * Get the validation rule to apply to a function. * -- 2.25.1