core#2309: Validate weight and weight threshold
authorMonish Deb <monish.deb@jmaconsulting.biz>
Mon, 15 Feb 2021 16:06:11 +0000 (21:36 +0530)
committerMonish Deb <monish.deb@jmaconsulting.biz>
Tue, 16 Feb 2021 03:12:20 +0000 (08:42 +0530)
CRM/Contact/Form/DedupeRules.php

index 7e4a850c9756f078ca9a01bb8b560c7dd330b424..c99ea8195e6fda79ce84b3207465a99c880a44a9 100644 (file)
@@ -136,11 +136,15 @@ class CRM_Contact_Form_DedupeRules extends CRM_Admin_Form {
   public static function formRule($fields, $files, $self) {
     $errors = [];
     $fieldSelected = FALSE;
+    $actualThreshold = 0;
     for ($count = 0; $count < self::RULES_COUNT; $count++) {
       if (!empty($fields["where_$count"]) || (isset($self->_defaults['is_reserved']) && !empty($self->_defaults["where_$count"]))) {
         $fieldSelected = TRUE;
         break;
       }
+      if (!empty($self->_defaults["weight_$count"])) {
+        $actualThreshold += $self->_defaults["weight_$count"];
+      }
     }
     if (empty($fields['threshold'])) {
       // CRM-20607 - Don't validate the threshold of hard-coded rules
@@ -149,6 +153,11 @@ class CRM_Contact_Form_DedupeRules extends CRM_Admin_Form {
         $errors['threshold'] = ts('Threshold weight cannot be empty or zero.');
       }
     }
+    else {
+      if ($actualThreshold < $fields['threshold']) {
+        $errors['threshold'] = ts('Total weight must be greater than or equal to the Weight Threshold.');
+      }
+    }
 
     if (!$fieldSelected) {
       $errors['_qf_default'] = ts('Please select at least one field.');