Skip to content

Commit

Permalink
Merge pull request #911 from GSA/910-validate-date-in-benefit-form-el…
Browse files Browse the repository at this point in the history
…igibility

910 validate date in Benefit form acceptable value
  • Loading branch information
scottqueen-bixal authored Feb 21, 2024
2 parents 8717183 + 7f31656 commit 87d9c7e
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ function usagov_benefit_finder_content_node_bears_benefit_edit_form_validate(arr
}
}
}

if (in_array($criteria_type, ['Date'])) {
foreach ($acceptable_values as $value) {
if (!empty($value)) {
if (strpos($value, ' ') !== FALSE) {
$errors[] = t("Criteria \"$criteria_title\" acceptable value \"$value\" cannot contain space.");
continue;
}
$pattern = '/^(=|>|<|>=|<=)(\d{2}-\d{2}-\d{4}|\d{1,2}years)$/';
if (preg_match($pattern, $value, $matches) === 1) {
$secondPart = $matches[2];
if (preg_match('/^\d{2}-\d{2}-\d{4}$/', $secondPart, $matches) === 1) {
$dateTime = DateTime::createFromFormat('m-d-Y', $secondPart);
if (!($dateTime && $dateTime->format('m-d-Y') === $secondPart)) {
$errors[] = t("Criteria \"$criteria_title\" acceptable value \"$value\" date \"$secondPart\" is invalid.");
$errors[] = t("--- Format: MM-DD-YYYY");
}
}
}
else {
$errors[] = t("Criteria \"$criteria_title\" acceptable value \"$value\" is incorrect.");
$errors[] = t("---The prefix must include only one of the following comparison operators: '>', '<', '=', '>=', or '<='.

--- The suffix must be formatted as either 'MM-DD-YYYY' or '(d or dd)years'.");
}
}
}
}
}

if (!empty($errors)) {
Expand Down

0 comments on commit 87d9c7e

Please sign in to comment.