];
$this->addButtons($buttons);
+
+ $this->addFormRule(['CRM_Campaign_Form_Campaign', 'formRule']);
}
/**
public static function formRule($fields, $files, $errors) {
$errors = [];
+ // Validate start/end date inputs
+ $validateDates = \CRM_Utils_Date::validateStartEndDatepickerInputs('start_date', $fields['start_date'], 'end_date', $fields['end_date']);
+ if ($validateDates !== TRUE) {
+ $errors[$validateDates['key']] = $validateDates['message'];
+ }
+
return empty($errors) ? TRUE : $errors;
}
}
}
- //CRM-11494
- $start = CRM_Utils_Date::processDate($values['start_date']);
- $end = CRM_Utils_Date::processDate($values['end_date']);
- if (($end < $start) && ($end != 0)) {
- $errors['end_date'] = ts('End date should be after Start date.');
+ // Validate start/end date inputs
+ $validateDates = \CRM_Utils_Date::validateStartEndDatepickerInputs('start_date', $values['start_date'], 'end_date', $values['end_date']);
+ if ($validateDates !== TRUE) {
+ $errors[$validateDates['key']] = $validateDates['message'];
}
if (!empty($self->_values['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) {
public static function formRule($values) {
$errors = [];
- if (!empty($values['end_date']) && ($values['end_date'] < $values['start_date'])) {
- $errors['end_date'] = ts('End date should be after Start date.');
+ // Validate start/end date inputs
+ $validateDates = \CRM_Utils_Date::validateStartEndDatepickerInputs('start_date', $values['start_date'], 'end_date', $values['end_date']);
+ if ($validateDates !== TRUE) {
+ $errors[$validateDates['key']] = $validateDates['message'];
}
//CRM-4286
}
}
- if (isset($values['registration_start_date']) && isset($values['registration_end_date'])) {
- if ($values['registration_end_date'] < $values['registration_start_date']) {
- $errorMsg['registration_end_date'] = ts('Registration end date should be after Registration start date');
- }
+ // Validate start/end date inputs
+ $validateDates = \CRM_Utils_Date::validateStartEndDatepickerInputs('registration_start_date', $values['registration_start_date'], 'registration_end_date', $values['registration_end_date']);
+ if ($validateDates !== TRUE) {
+ $errorMsg[$validateDates['key']] = $validateDates['message'];
}
//check that the selected profiles have either firstname+lastname or email required
$errors['count'] = ts('Participant Count must be greater than zero.');
}
+ // Validate start/end date inputs
+ $validateDates = \CRM_Utils_Date::validateStartEndDatepickerInputs('active_on', $fields['active_on'], 'expire_on', $fields['expire_on']);
+ if ($validateDates !== TRUE) {
+ $errors[$validateDates['key']] = $validateDates['message'];
+ }
+
if ($form->_action & CRM_Core_Action::ADD) {
if ($fields['html_type'] != 'Text') {
$countemptyrows = 0;
return $dateObject->format($format);
}
+ /**
+ * Check if the value returned by a date picker has a date section (ie: includes
+ * a '-' character) if it includes a time section (ie: includes a ':').
+ *
+ * @param string $value
+ * A date/time string input from a datepicker value.
+ *
+ * @return bool
+ * TRUE if valid, FALSE if there is a time without a date.
+ */
+ public static function datePickerValueWithTimeHasDate($value) {
+ // If there's no : (time) or a : and a - (date) then return true
+ return (
+ strpos($value, ':') === FALSE
+ || strpos($value, ':') !== FALSE && strpos($value, '-') !== FALSE
+ );
+ }
+
+ /**
+ * Validate start and end dates entered on a form to make sure they are
+ * logical. Expects the form keys to be start_date and end_date.
+ *
+ * @param string $startFormKey
+ * The form element key of the 'start date'
+ * @param string $startValue
+ * The value of the 'start date'
+ * @param string $endFormKey
+ * The form element key of the 'end date'
+ * @param string $endValue
+ * The value of the 'end date'
+ *
+ * @return array|bool
+ * TRUE if valid, an array of the erroneous form key, and error message to
+ * use otherwise.
+ */
+ public static function validateStartEndDatepickerInputs($startFormKey, $startValue, $endFormKey, $endValue) {
+
+ // Check date as well as time is set
+ if (!empty($startValue) && !self::datePickerValueWithTimeHasDate($startValue)) {
+ return ['key' => $startFormKey, 'message' => ts('Please enter a date as well as a time.')];
+ }
+ if (!empty($endValue) && !self::datePickerValueWithTimeHasDate($endValue)) {
+ return ['key' => $endFormKey, 'message' => ts('Please enter a date as well as a time.')];
+ }
+
+ // Check end date is after start date
+ if (!empty($startValue) && !empty($endValue) && $endValue < $startValue) {
+ return ['key' => $endFormKey, 'message' => ts('The end date should be after the start date.')];
+ }
+
+ return TRUE;
+ }
+
}