Main frame scroll bar fix (#4703)
[openemr.git] / src / Validators / PatientValidator.php
blob28e21066b0473ad84e91a6d2ac4820ad0373140a
1 <?php
3 namespace OpenEMR\Validators;
5 use Particle\Validator\Validator;
6 use Particle\Validator\Exception\InvalidValueException;
7 use OpenEMR\Common\Uuid\UuidRegistry;
8 use Ramsey\Uuid\Exception\InvalidUuidStringException;
10 /**
11 * Supports Patient Record Validation.
13 * @package OpenEMR
14 * @link http://www.open-emr.org
15 * @author Dixon Whitmire <dixonwh@gmail.com>
16 * @copyright Copyright (c) 2020 Jerry Padgett <sjpadgett@gmail.com>
17 * @copyright Copyright (c) 2020 Dixon Whitmire <dixonwh@gmail.com>
18 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
20 class PatientValidator extends BaseValidator
22 /**
23 * Validates that a patient UUID exists in the database
25 public function isExistingUuid($uuid)
27 try {
28 $uuidLookup = UuidRegistry::uuidToBytes($uuid);
29 } catch (InvalidUuidStringException $e) {
30 return false;
33 $result = sqlQuery(
34 'SELECT uuid AS uuid FROM patient_data WHERE uuid = ?',
35 array($uuidLookup)
38 $existingUuid = $result['uuid'] ?? null;
39 return $existingUuid != null;
42 /**
43 * Configures validations for the Patient DB Insert and Update use-case.
44 * The update use-case is comprised of the same fields as the insert use-case.
45 * The update use-case differs from the insert use-case in that fields other than pid are not required.
47 protected function configureValidator()
49 parent::configureValidator();
51 // insert validations
52 $this->validator->context(
53 self::DATABASE_INSERT_CONTEXT,
54 function (Validator $context) {
55 $context->required("fname", "First Name")->lengthBetween(2, 255);
56 $context->required("lname", 'Last Name')->lengthBetween(2, 255);
57 $context->required("sex", 'Gender')->lengthBetween(4, 30);
58 $context->required("DOB", 'Date of Birth')->datetime('Y-m-d');
62 // update validations copied from insert
63 $this->validator->context(
64 self::DATABASE_UPDATE_CONTEXT,
65 function (Validator $context) {
66 $context->copyContext(
67 self::DATABASE_INSERT_CONTEXT,
68 function ($rules) {
69 foreach ($rules as $key => $chain) {
70 $chain->required(false);
74 // additional uuid validations
75 $context->required("uuid", "uuid")->callback(function ($value) {
76 if (!$this->isExistingUuid($value)) {
77 $message = "UUID " . $value . " does not exist";
78 throw new InvalidValueException($message, $value);
80 return true;
81 })->string();