Minor simplification on additional_participants checks
authoreileen <emcnaughton@wikimedia.org>
Mon, 29 Jul 2019 03:52:19 +0000 (15:52 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 9 Dec 2019 06:37:21 +0000 (19:37 +1300)
CRM/Event/Form/Registration/Register.php

index 131e08eb2817631c90ad957af4351605f779ad47..734df5ff51c46c6b81f7598ca96ab61e41ee67c3 100644 (file)
@@ -807,10 +807,16 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       $errors['additional_participants'] = ts("There is only enough space left on this event for %1 participant(s).", [1 => $form->_availableRegistrations]);
     }
 
+    $numberAdditionalParticipants = $fields['additional_participants'] ?? 0;
+
+    if ($numberAdditionalParticipants && !CRM_Utils_Rule::positiveInteger($fields['additional_participants'])) {
+      $errors['additional_participants'] = ts('Please enter a whole number for Number of additional people.');
+    }
+
     // during confirmation don't allow to increase additional participants, CRM-4320
-    if ($form->_allowConfirmation && !empty($fields['additional_participants']) &&
+    if ($form->_allowConfirmation && $numberAdditionalParticipants &&
       is_array($form->_additionalParticipantIds) &&
-      $fields['additional_participants'] > count($form->_additionalParticipantIds)
+      $numberAdditionalParticipants > count($form->_additionalParticipantIds)
     ) {
       $errors['additional_participants'] = ts("Oops. It looks like you are trying to increase the number of additional people you are registering for. You can confirm registration for a maximum of %1 additional people.", [1 => count($form->_additionalParticipantIds)]);
     }
@@ -824,12 +830,6 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       }
     }
 
-    if (!empty($fields['additional_participants']) &&
-      !CRM_Utils_Rule::positiveInteger($fields['additional_participants'])
-    ) {
-      $errors['additional_participants'] = ts('Please enter a whole number for Number of additional people.');
-    }
-
     // priceset validations
     if (!empty($fields['priceSetId']) &&
      !$form->_requireApproval && !$form->_allowWaitlist
@@ -844,8 +844,8 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       $errors = array_merge($errors, CRM_Utils_Array::value(0, $priceSetErrors, []));
 
       $totalParticipants = $primaryParticipantCount;
-      if (!empty($fields['additional_participants'])) {
-        $totalParticipants += $fields['additional_participants'];
+      if ($numberAdditionalParticipants) {
+        $totalParticipants += $numberAdditionalParticipants;
       }
 
       if (empty($fields['bypass_payment']) &&