‪TYPO3CMS  9.5
DateRangeValidator.php
Go to the documentation of this file.
1 <?php
2 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 
20 
27 {
31  protected ‪$supportedOptions = [
32  'minimum' => ['', 'The minimum date formatted as Y-m-d', 'string'],
33  'maximum' => ['', 'The maximum date formatted as Y-m-d', 'string'],
34  'format' => ['Y-m-d', 'The format of the minimum and maximum option', 'string'],
35  ];
36 
40  public function ‪isValid($value)
41  {
42  $this->‪validateOptions();
43 
44  if (!($value instanceof \DateTime)) {
45  $this->‪addError(
47  'validation.error.1521293685',
48  'form',
49  [gettype($value)]
50  ),
51  1521293685
52  );
53 
54  return;
55  }
56 
57  $minimum = $this->options['minimum'];
58  $maximum = $this->options['maximum'];
59  $format = $this->options['format'];
60  $value->modify('midnight');
61 
62  if (
63  $minimum instanceof \DateTime
64  && $value < $minimum
65  ) {
66  $this->‪addError(
68  'validation.error.1521293686',
69  'form',
70  [$minimum->format($format)]
71  ),
72  1521293686,
73  [$minimum->format($format)]
74  );
75  }
76 
77  if (
78  $maximum instanceof \DateTime
79  && $value > $maximum
80  ) {
81  $this->‪addError(
83  'validation.error.1521293687',
84  'form',
85  [$maximum->format($format)]
86  ),
87  1521293687,
88  [$maximum->format($format)]
89  );
90  }
91  }
92 
98  protected function ‪validateOptions()
99  {
100  if (!empty($this->options['minimum'])) {
101  $minimum = \DateTime::createFromFormat($this->options['format'], $this->options['minimum']);
102  if (!($minimum instanceof \DateTime)) {
103  $message = sprintf('The option "minimum" (%s) could not be converted to \DateTime from format "%s".', $this->options['minimum'], $this->options['format']);
104  throw new ‪InvalidValidationOptionsException($message, 1521293813);
105  }
106 
107  $minimum->modify('midnight');
108  $this->options['minimum'] = $minimum;
109  }
110 
111  if (!empty($this->options['maximum'])) {
112  $maximum = \DateTime::createFromFormat($this->options['format'], $this->options['maximum']);
113  if (!($maximum instanceof \DateTime)) {
114  $message = sprintf('The option "maximum" (%s) could not be converted to \DateTime from format "%s".', $this->options['maximum'], $this->options['format']);
115  throw new ‪InvalidValidationOptionsException($message, 1521293814);
116  }
117 
118  $maximum->modify('midnight');
119  $this->options['maximum'] = $maximum;
120  }
121  }
122 }
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:23
‪TYPO3\CMS\Form\Mvc\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:23
‪TYPO3\CMS\Form\Mvc\Validation\DateRangeValidator\$supportedOptions
‪array $supportedOptions
Definition: DateRangeValidator.php:30
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\translateErrorMessage
‪string null translateErrorMessage($translateKey, $extensionName, $arguments=[])
Definition: AbstractValidator.php:149
‪TYPO3\CMS\Form\Mvc\Validation\DateRangeValidator\validateOptions
‪validateOptions()
Definition: DateRangeValidator.php:97
‪TYPO3\CMS\Form\Mvc\Validation
Definition: CountValidator.php:3
‪TYPO3\CMS\Form\Mvc\Validation\DateRangeValidator
Definition: DateRangeValidator.php:27
‪TYPO3\CMS\Form\Mvc\Validation\DateRangeValidator\isValid
‪isValid($value)
Definition: DateRangeValidator.php:39
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\addError
‪addError($message, $code, array $arguments=[], $title='')
Definition: AbstractValidator.php:116