‪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 {
27  protected string ‪$message = 'LLL:EXT:extbase/Resources/Private/Language/locallang.xlf:validator.regularexpression.nomatch';
28 
32  protected ‪$supportedOptions = [
33  'regularExpression' => ['', 'The regular expression to use for validation, used as given', 'string', true],
34  'errorMessage' => ['', 'Error Message to show when validation fails', 'string', false],
35  'message' => [null, 'Translation key or message when regular expression results in a no match', 'string'],
36  ];
37 
43  public function ‪isValid(mixed $value): void
44  {
45  ‪$result = preg_match($this->options['regularExpression'], $value);
46  if (‪$result === 0) {
47  $this->‪addError(
48  $this->‪getErrorMessage(),
49  1221565130
50  );
51  }
52  if (‪$result === false) {
53  throw new ‪InvalidValidationOptionsException('regularExpression "' . $this->options['regularExpression'] . '" in RegularExpressionValidator contained an error.', 1298273089);
54  }
55  }
56 
57  protected function ‪getErrorMessage(): string
58  {
59  $errorMessage = (string)($this->options['errorMessage'] ?? '');
60  if ($errorMessage !== '') {
61  trigger_error(
62  'Validator option "errorMessage" is deprecated and will be removed in TYPO3 v14.0, use "message" option instead.',
63  E_USER_DEPRECATED
64  );
65  }
66 
67  // if custom message is no locallang reference
68  if ($errorMessage !== '' && !str_starts_with($errorMessage, 'LLL')) {
69  return $errorMessage;
70  }
71  if ($errorMessage === '') {
72  // fallback to default message
73  $errorMessage = ‪$this->message;
74  }
75 
76  return $this->‪translateErrorMessage($errorMessage);
77  }
78 }
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:30
‪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:98
‪TYPO3\CMS\Extbase\Validation\Validator\RegularExpressionValidator\$message
‪string $message
Definition: RegularExpressionValidator.php:27
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$result
‪Result $result
Definition: AbstractValidator.php:53
‪TYPO3\CMS\Extbase\Validation\Validator\RegularExpressionValidator\$supportedOptions
‪array $supportedOptions
Definition: RegularExpressionValidator.php:31
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\translateErrorMessage
‪translateErrorMessage(string $translateKey, string $extensionName='', array $arguments=[])
Definition: AbstractValidator.php:147
‪TYPO3\CMS\Extbase\Validation\Validator\RegularExpressionValidator\isValid
‪isValid(mixed $value)
Definition: RegularExpressionValidator.php:42
‪TYPO3\CMS\Extbase\Validation\Validator\RegularExpressionValidator\getErrorMessage
‪getErrorMessage()
Definition: RegularExpressionValidator.php:56
‪TYPO3\CMS\Extbase\Validation\Validator
Definition: AbstractCompositeValidator.php:18
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:25