Skip to content

Commit

Permalink
chore: also update other forms that need new TU/e-username validator
Browse files Browse the repository at this point in the history
  • Loading branch information
tomudding committed Sep 2, 2024
1 parent ee89eea commit 8174b3b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions module/Database/src/Form/MemberEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
use Laminas\Form\Form;
use Laminas\InputFilter\InputFilterProviderInterface;
use Laminas\Mvc\I18n\Translator;
use Laminas\Validator\Callback;
use Laminas\Validator\Digits;
use Laminas\Validator\EmailAddress;
use Laminas\Validator\Regex;
use Laminas\Validator\StringLength;
use Throwable;

use function date;
use function preg_match;
use function substr;

class MemberEdit extends Form implements InputFilterProviderInterface
{
Expand Down Expand Up @@ -203,6 +209,20 @@ public function getInputFilterSpecification(): array
],
],
],
[
'name' => Callback::class,
'options' => [
'callback' => function ($value) {
return $this->isNewTueUsernameValid($value);
},
'messages' => [
Callback::INVALID_VALUE => $this->translator->translate(
// phpcs:ignore -- user-visible strings should not be split
'Your TU/e-username appears to be incorrect. Ensure that it starts with a valid year and looks like: YYYYxxxx. If you believe your TU/e-username is correct, please contact the secretary.',
),
],
],
],
],
'filters' => [
['name' => ToNull::class],
Expand All @@ -222,4 +242,23 @@ public function getInputFilterSpecification(): array
],
];
}

private function isNewTueUsernameValid(string $value): bool
{
try {
// Only check for YYYYABCD TU/e usernames.
if (preg_match('/^s\d{6}$/', $value)) {
return true;
}

$year = substr($value, 0, 4);
$currentYear = date('Y');

// Check if the year is within the valid range, the assumption being that you can never have a number
// starting with a year that is higher than the current year.
return $year >= 2000 && $year <= $currentYear;
} catch (Throwable) {
return false;
}
}
}

0 comments on commit 8174b3b

Please sign in to comment.