‪TYPO3CMS  ‪main
RegularExpressionValidator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
26 {
30  protected ‪$supportedOptions = [
31  'regularExpression' => ['', 'The regular expression to use for validation, used as given', 'string', true],
32  'errorMessage' => ['', 'Error Message to show when validation fails', 'string', false],
33  ];
34 
40  public function ‪isValid(mixed $value): void
41  {
42  ‪$result = preg_match($this->options['regularExpression'], $value);
43  if (‪$result === 0) {
44  $errorMessage = $this->‪getErrorMessage();
45  $this->‪addError(
46  $errorMessage,
47  1221565130
48  );
49  }
50  if (‪$result === false) {
51  throw new ‪InvalidValidationOptionsException('regularExpression "' . $this->options['regularExpression'] . '" in RegularExpressionValidator contained an error.', 1298273089);
52  }
53  }
54 
55  protected function ‪getErrorMessage(): string
56  {
57  $errorMessage = (string)($this->options['errorMessage'] ?? '');
58  // if custom message is no locallang reference
59  if ($errorMessage !== '' && !str_starts_with($errorMessage, 'LLL')) {
60  return $errorMessage;
61  }
62  if ($errorMessage === '') {
63  // fallback to default message
64  $errorMessage = 'validator.regularexpression.nomatch';
65  }
66  return $this->‪translateErrorMessage(
67  $errorMessage,
68  'extbase'
69  );
70  }
71 }
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:29
‪TYPO3\CMS\Extbase\Validation\Validator\RegularExpressionValidator
Definition: RegularExpressionValidator.php:26
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\addError
‪addError(string $message, int $code, array $arguments=[], string $title='')
Definition: AbstractValidator.php:82
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\translateErrorMessage
‪translateErrorMessage(string $translateKey, string $extensionName, array $arguments=[])
Definition: AbstractValidator.php:122
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$result
‪Result $result
Definition: AbstractValidator.php:47
‪TYPO3\CMS\Extbase\Validation\Validator\RegularExpressionValidator\$supportedOptions
‪array $supportedOptions
Definition: RegularExpressionValidator.php:29
‪TYPO3\CMS\Extbase\Validation\Validator\RegularExpressionValidator\isValid
‪isValid(mixed $value)
Definition: RegularExpressionValidator.php:39
‪TYPO3\CMS\Extbase\Validation\Validator\RegularExpressionValidator\getErrorMessage
‪getErrorMessage()
Definition: RegularExpressionValidator.php:54
‪TYPO3\CMS\Extbase\Validation\Validator
Definition: AbstractCompositeValidator.php:18
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:25