From 95e645e397cf91349d6d5a0cf2fc5e4ac01b7888 Mon Sep 17 00:00:00 2001 From: regisoc Date: Thu, 15 Dec 2022 22:58:29 -0500 Subject: [PATCH] Valid date message when date is invalid --- modules/api/php/endpoints/candidates.class.inc | 2 +- php/libraries/Candidate.class.inc | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/modules/api/php/endpoints/candidates.class.inc b/modules/api/php/endpoints/candidates.class.inc index 63281e3bf73..64ae7d6c7e2 100644 --- a/modules/api/php/endpoints/candidates.class.inc +++ b/modules/api/php/endpoints/candidates.class.inc @@ -241,7 +241,7 @@ class Candidates extends Endpoint implements \LORIS\Middleware\ETagCalculator $pscid, $project->getId() ); - } catch (\LorisException $e) { + } catch (\LorisException | \InvalidArgumentException $e) { return new \LORIS\Http\Response\JSON\BadRequest($e->getMessage()); } diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index 8de3188368e..cf987cf047b 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -18,6 +18,7 @@ define('PSCID_NOT_UNIQUE', 3); define('PSCID_INVALID_STRUCTURE', 4); define('EDC_NOT_SPECIFIED', 5); define('DOB_NOT_SPECIFIED', 6); +define('DOB_INVALID', 7); /** * Wrapper around a candidate in Loris. Mostly, it gets information @@ -240,15 +241,19 @@ class Candidate implements \LORIS\StudyEntities\AccessibleResource, EDC_NOT_SPECIFIED ); } - if ($dateOfBirth !== null - && (DateTime::createFromFormat('Y-m-d', $dateOfBirth) === false - || empty($dateOfBirth)) - ) { + if (empty($dateOfBirth)) { throw new InvalidArgumentException( "Date of Birth must be specified", DOB_NOT_SPECIFIED ); } + $dob = DateTime::createFromFormat('!Y-m-d', $dateOfBirth); + if ($dob === false) { + throw new InvalidArgumentException( + "Date of Birth is invalid (expected format: YYYY-MM-DD)", + DOB_INVALID + ); + } if ($PSCIDSettings['generation'] == 'user') { // check pscid is specified