/** * global form rule * * @param array $fields the input form values * @param array $files the uploaded files if any * @param array $options additional user data * * @return true if no errors, else array of errors * @access public * @static */ static function formRule(&$fields, &$files, &$self) { //To check if the user is already registered for the event(CRM-2426) self::checkRegistration($fields, $self); //check for availability of registrations. if (!$self->_allowConfirmation && !CRM_Utils_Array::value('bypass_payment', $fields) && is_numeric($self->_availableRegistrations) && CRM_Utils_Array::value('additional_participants', $fields) >= $self->_availableRegistrations) { $errors['additional_participants'] = ts("There is only enough space left on this event for %1 participant(s).", array(1 => $self->_availableRegistrations)); } // during confirmation don't allow to increase additional participants, CRM-4320 if ($self->_allowConfirmation && CRM_Utils_Array::value('additional_participants', $fields) && is_array($self->_additionalParticipantIds) && $fields['additional_participants'] > count($self->_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.", array(1 => count($self->_additionalParticipantIds))); } //don't allow to register w/ waiting if enough spaces available. if (CRM_Utils_Array::value('bypass_payment', $fields)) { if (!is_numeric($self->_availableRegistrations) || CRM_Utils_Array::value('additional_participants', $fields) < $self->_availableRegistrations) { $errors['bypass_payment'] = ts("Oops. There are enough available spaces in this event. You can not add yourself to the waiting list."); } } $email = $fields["email-{$self->_bltID}"]; require_once 'CRM/Core/BAO/UFMatch.php'; if (CRM_Core_BAO_UFMatch::isDuplicateUser($email)) { $errors["email-{$self->_bltID}"] = ts('The email %1 already exists in the database.', array(1 => $email)); } if (CRM_Utils_Array::value('additional_participants', $fields) && !CRM_Utils_Rule::positiveInteger($fields['additional_participants'])) { $errors['additional_participants'] = ts('Please enter a whole number for Number of additional people.'); } //check for atleast one pricefields should be selected if (CRM_Utils_Array::value('priceSetId', $fields)) { $priceField = new CRM_Price_DAO_Field(); $priceField->price_set_id = $fields['priceSetId']; $priceField->find(); $check = array(); while ($priceField->fetch()) { if (!empty($fields["price_{$priceField->id}"])) { $check[] = $priceField->id; } } if (empty($check)) { $errors['_qf_default'] = ts("Select at least one option from Event Fee(s)."); } $lineItem = array(); require_once "CRM/Price/BAO/Set.php"; CRM_Price_BAO_Set::processAmount($self->_values['fee']['fields'], $fields, $lineItem); if ($fields['amount'] < 0) { $errors['_qf_default'] = ts("Event Fee(s) can not be less than zero. Please select the options accordingly"); } } if ($self->_values['event']['is_monetary']) { if (is_array($self->_paymentProcessor)) { $payment =& CRM_Core_Payment::singleton($self->_mode, 'Event', $self->_paymentProcessor, $this); $error = $payment->checkConfig($self->_mode); if ($error) { $errors['_qf_default'] = $error; } } // return if this is express mode $config =& CRM_Core_Config::singleton(); if ($self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) { if (CRM_Utils_Array::value($self->_expressButtonName . '_x', $fields) || CRM_Utils_Array::value($self->_expressButtonName . '_y', $fields) || CRM_Utils_Array::value($self->_expressButtonName, $fields)) { return empty($errors) ? true : $errors; } } $zeroAmount = $fields['amount']; // also return if paylater mode or zero fees for valid members if (CRM_Utils_Array::value('is_pay_later', $fields) || CRM_Utils_Array::value('bypass_payment', $fields)) { if (CRM_Utils_Array::value('priceSetId', $fields)) { foreach ($fields as $key => $val) { if (substr($key, 0, 6) == 'price_' && $val != 0) { return empty($errors) ? true : $errors; } } } else { return empty($errors) ? true : $errors; } } else { if (CRM_Utils_Array::value('priceSetId', $fields)) { //here take all value(amount) of optoin value id $check = array(); foreach ($fields as $key => $val) { if (substr($key, 0, 6) == 'price_' && $val != 0) { $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Field', substr($key, 6), 'html_type'); if (is_array($val)) { //$keys is the id of the option value foreach ($val as $keys => $vals) { if (!$keys) { continue; } $check[] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $keys, 'name'); } } else { if ($htmlType == 'Text') { $check[] = $val; } else { //$val is the id of the option value $check[] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $val, 'name'); } } } } //validation for submitted each value is zero //if not zero give credit card validation error else //bypass it. $level = count($check); $j = 0; for ($i = 0; $i < $level; $i++) { if ($check[$i] >= 0) { $j += $check[$i]; } } if ($j == 0) { return empty($errors) ? true : $errors; } } else { if ($zeroAmount) { if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $zeroAmount, 'value', 'id') == 0) { return empty($errors) ? true : $errors; } } } } //is pay later and priceset is used avoid credit card and //billing address validation if ((CRM_Utils_Array::value('is_pay_later', $fields) || CRM_Utils_Array::value('bypass_payment', $fields)) && $fields['priceSetId'] || !$self->_allowConfirmation && ($self->_requireApproval || $self->_allowWaitlist)) { return empty($errors) ? true : $errors; } foreach ($self->_fields as $name => $fld) { if ($fld['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) { $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title'])); } } } // make sure that credit card number and cvv are valid require_once 'CRM/Utils/Rule.php'; if (CRM_Utils_Array::value('credit_card_type', $fields)) { if (CRM_Utils_Array::value('credit_card_number', $fields) && !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type'])) { $errors['credit_card_number'] = ts("Please enter a valid Credit Card Number"); } if (CRM_Utils_Array::value('cvv2', $fields) && !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type'])) { $errors['cvv2'] = ts("Please enter a valid Credit Card Verification Number"); } } $elements = array('email_greeting' => 'email_greeting_custom', 'postal_greeting' => 'postal_greeting_custom', 'addressee' => 'addressee_custom'); foreach ($elements as $greeting => $customizedGreeting) { if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) { $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name'); if ($customizedValue == $greetingType && !CRM_Utils_Array::value($customizedGreeting, $fields)) { $errors[$customizedGreeting] = ts('Custom %1 is a required field if %1 is of type Customized.', array(1 => ucwords(str_replace('_', " ", $greeting)))); } } } return empty($errors) ? true : $errors; }
/** * global form rule * * @param array $fields the input form values * @param array $files the uploaded files if any * @param array $options additional user data * * @return true if no errors, else array of errors * @access public * @static */ static function formRule(&$fields, &$files, &$self) { $errors = array(); $amount = self::computeAmount($fields, $self); $email = $fields["email-{$self->_bltID}"]; require_once 'CRM/Core/BAO/UFMatch.php'; if (CRM_Core_BAO_UFMatch::isDuplicateUser($email)) { $errors["email-{$self->_bltID}"] = ts('The email %1 already exists in the database.', array(1 => $email)); } //check for atleast one pricefields should be selected if (CRM_Utils_Array::value('priceSetId', $fields)) { $priceField = new CRM_Price_DAO_Field(); $priceField->price_set_id = $fields['priceSetId']; $priceField->find(); $check = array(); while ($priceField->fetch()) { if (!empty($fields["price_{$priceField->id}"])) { $check[] = $priceField->id; } } if (empty($check)) { $errors['_qf_default'] = ts("Select at least one option from Contribution(s)."); } require_once 'CRM/Price/BAO/Set.php'; CRM_Price_BAO_Set::processAmount($self->_priceSet['fields'], $fields, $lineItem); if ($fields['amount'] < 0) { $errors['_qf_default'] = ts("Contribution can not be less than zero. Please select the options accordingly"); } $amount = $fields['amount']; } if (isset($fields['selectProduct']) && $fields['selectProduct'] != 'no_thanks' && $self->_values['amount_block_is_active']) { require_once 'CRM/Contribute/DAO/Product.php'; require_once 'CRM/Utils/Money.php'; $productDAO =& new CRM_Contribute_DAO_Product(); $productDAO->id = $fields['selectProduct']; $productDAO->find(true); $min_amount = $productDAO->min_contribution; if ($amount < $min_amount) { $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($min_amount))); } } if ($self->_values["honor_block_is_active"] && CRM_Utils_Array::value('honor_type_id', $fields)) { // make sure there is a first name and last name if email is not there if (!CRM_Utils_Array::value('honor_email', $fields)) { if (!CRM_Utils_Array::value('honor_first_name', $fields) || !CRM_Utils_Array::value('honor_last_name', $fields)) { $errors['honor_last_name'] = ts('In Honor Of - First Name and Last Name, OR an Email Address is required.'); } } } if (isset($fields['is_recur']) && $fields['is_recur']) { if ($fields['frequency_interval'] <= 0) { $errors['frequency_interval'] = ts('Please enter a number for how often you want to make this recurring contribution (EXAMPLE: Every 3 months).'); } if ($fields['frequency_unit'] == '0') { $errors['frequency_unit'] = ts('Please select a period (e.g. months, years ...) for how often you want to make this recurring contribution (EXAMPLE: Every 3 MONTHS).'); } } if (CRM_Utils_Array::value('is_recur', $fields) && $fields['is_pay_later']) { $errors['is_pay_later'] = ' '; $errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.'); } if (CRM_Utils_Array::value('is_for_organization', $fields)) { if (CRM_Utils_Array::value('org_option', $fields) && !$fields['onbehalfof_id']) { $errors['organization_id'] = ts('Please select an organization or enter a new one.'); } if (!CRM_Utils_Array::value('org_option', $fields) && !$fields['organization_name']) { $errors['organization_name'] = ts('Please enter the organization name.'); } if (!$fields['email'][1]['email']) { $errors["email[1][email]"] = ts('Organization email is required.'); } } if (CRM_Utils_Array::value('selectMembership', $fields) && $fields['selectMembership'] != 'no_thanks') { require_once 'CRM/Member/BAO/Membership.php'; require_once 'CRM/Member/BAO/MembershipType.php'; $memTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($fields['selectMembership']); if ($self->_values['amount_block_is_active'] && !CRM_Utils_Array::value('is_separate_payment', $self->_membershipBlock)) { require_once 'CRM/Utils/Money.php'; if ($amount < CRM_Utils_Array::value('minimum_fee', $memTypeDetails)) { $errors['selectMembership'] = ts('The Membership you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($memTypeDetails['minimum_fee']))); } } else { if (CRM_Utils_Array::value('minimum_fee', $memTypeDetails)) { // we dont have an amount, so lets get an amount for cc checks $amount = $memTypeDetails['minimum_fee']; } } } if ($self->_values['is_monetary']) { if (CRM_Utils_Array::value('amount', $fields) == 'amount_other_radio' || isset($fields['amount_other'])) { if (!isset($amount) && !CRM_Utils_Array::value('amount_level', $fields)) { $errors['amount_other'] = ts('Amount is required field.'); } if (CRM_Utils_Array::value('min_amount', $self->_values)) { $min = $self->_values['min_amount']; if ($fields['amount_other'] != '' && $fields['amount_other'] < $min) { $errors['amount_other'] = ts('Contribution amount must be greater than %1', array(1 => $min)); } } if (CRM_Utils_Array::value('max_amount', $self->_values) > 0) { $max = $self->_values['max_amount']; if ($fields['amount_other'] > $max) { $errors['amount_other'] = ts('Contribution amount cannot be greater than %1.', array(1 => $max)); } } } } // validate PCP fields - if not anonymous, we need a nick name value if ($self->_pcpId && CRM_Utils_Array::value('pcp_display_in_roll', $fields) && CRM_Utils_Array::value('pcp_is_anonymous', $fields) == 0 && CRM_Utils_Array::value('pcp_roll_nickname', $fields) == '') { $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.'); } // return if this is express mode $config =& CRM_Core_Config::singleton(); if ($self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) { if (CRM_Utils_Array::value($self->_expressButtonName . '_x', $fields) || CRM_Utils_Array::value($self->_expressButtonName . '_y', $fields) || CRM_Utils_Array::value($self->_expressButtonName, $fields)) { return $errors; } } //validate the pledge fields. if (CRM_Utils_Array::value('pledge_block_id', $self->_values)) { //validation for pledge payment. if (CRM_Utils_Array::value('pledge_id', $self->_values)) { if (empty($fields['pledge_amount'])) { $errors['pledge_amount'] = ts('At least one payment option needs to be checked.'); } } else { if (CRM_Utils_Array::value('is_pledge', $fields)) { if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_installments', $fields)) == false) { $errors['pledge_installments'] = ts('Please enter a valid pledge installment.'); } else { if (CRM_Utils_Array::value('pledge_installments', $fields) == null) { $errors['pledge_installments'] = ts('Pledge Installments is required field.'); } else { if (CRM_Utils_array::value('pledge_installments', $fields) == 1) { $errors['pledge_installments'] = ts('Pledges consist of multiple scheduled payments. Select one-time contribution if you want to make your gift in a single payment.'); } else { if (CRM_Utils_array::value('pledge_installments', $fields) == 0) { $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.'); } } } } //validation for Pledge Frequency Interval. if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_frequency_interval', $fields)) == false) { $errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.'); } else { if (CRM_Utils_Array::value('pledge_frequency_interval', $fields) == null) { $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.'); } else { if (CRM_Utils_array::value('pledge_frequency_interval', $fields) == 0) { $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0'); } } } } } } // also return if paylater mode if (CRM_Utils_Array::value('is_pay_later', $fields)) { return empty($errors) ? true : $errors; } // if the user has chosen a free membership or the amount is less than zero // i.e. we skip calling the payment processor and hence dont need credit card // info if ((double) $amount <= 0.0) { return $errors; } foreach ($self->_fields as $name => $fld) { if ($fld['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) { $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title'])); } } // make sure that credit card number and cvv are valid require_once 'CRM/Utils/Rule.php'; if (CRM_Utils_Array::value('credit_card_type', $fields)) { if (CRM_Utils_Array::value('credit_card_number', $fields) && !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type'])) { $errors['credit_card_number'] = ts("Please enter a valid Credit Card Number"); } if (CRM_Utils_Array::value('cvv2', $fields) && !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type'])) { $errors['cvv2'] = ts("Please enter a valid Credit Card Verification Number"); } } $elements = array('email_greeting' => 'email_greeting_custom', 'postal_greeting' => 'postal_greeting_custom', 'addressee' => 'addressee_custom'); foreach ($elements as $greeting => $customizedGreeting) { if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) { $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name'); if ($customizedValue == $greetingType && !CRM_Utils_Array::value($customizedGreeting, $fields)) { $errors[$customizedGreeting] = ts('Custom %1 is a required field if %1 is of type Customized.', array(1 => ucwords(str_replace('_', " ", $greeting)))); } } } return empty($errors) ? true : $errors; }