TYPO3 CMS  TYPO3_7-6
DateValidator.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 {
22  protected $supportedOptions = [
23  'element' => ['', 'The name of the element', 'string', true],
24  'errorMessage' => ['', 'The error message', 'array', true],
25  'format' => ['', 'The maximum value', 'string', true],
26  ];
27 
33  const LOCALISATION_OBJECT_NAME = 'tx_form_system_validate_date';
34 
42  public function isValid($value)
43  {
44  if (
45  $this->options['format'] === null
46  || $this->options['format'] === ''
47  ) {
48  $this->options['format'] = '%e-%m-%Y';
49  }
50 
51  if (function_exists('strptime')) {
52  $parsedDate = strptime($value, $this->options['format']);
53  $parsedDateYear = $parsedDate['tm_year'] + 1900;
54  $parsedDateMonth = $parsedDate['tm_mon'] + 1;
55  $parsedDateDay = $parsedDate['tm_mday'];
56  if (!checkdate($parsedDateMonth, $parsedDateDay, $parsedDateYear)) {
57  $this->addError(
58  $this->renderMessage(
59  $this->options['errorMessage'][0],
60  $this->options['errorMessage'][1],
61  'error'
62  ),
63  1442001386
64  );
65  return;
66  }
67  } else {
68  // %a => D : An abbreviated textual representation of the day (conversion works only for english)
69  // %A => l : A full textual representation of the day (conversion works only for english)
70  // %d => d : Day of the month, 2 digits with leading zeros
71  // %e => j : Day of the month, 2 digits without leading zeros
72  // %j => z : Day of the year, 3 digits with leading zeros
73  // %b => M : Abbreviated month name, based on the locale (conversion works only for english)
74  // %B => F : Full month name, based on the locale (conversion works only for english)
75  // %h => M : Abbreviated month name, based on the locale (an alias of %b) (conversion works only for english)
76  // %m => m : Two digit representation of the month
77  // %y => y : Two digit representation of the year
78  // %Y => Y : Four digit representation for the year
79  $dateTimeFormat = str_replace(
80  ['%a', '%A', '%d', '%e', '%j', '%b', '%B', '%h', '%m', '%y', '%Y'],
81  ['D', 'l', 'd', 'j', 'z', 'M', 'F', 'M', 'm', 'y', 'Y'],
82  $this->options['format']
83  );
84  $dateTimeObject = date_create_from_format($dateTimeFormat, $value);
85  if ($dateTimeObject === false) {
86  $this->addError(
87  $this->renderMessage(
88  $this->options['errorMessage'][0],
89  $this->options['errorMessage'][1],
90  'error'
91  ),
92  1442001386
93  );
94  return;
95  }
96 
97  if ($value !== $dateTimeObject->format($dateTimeFormat)) {
98  $this->addError(
99  $this->renderMessage(
100  $this->options['errorMessage'][0],
101  $this->options['errorMessage'][1],
102  'error'
103  ),
104  1442001386
105  );
106  }
107  }
108  }
109 
117  public function substituteMarkers($message)
118  {
119  $humanReadableDateFormat = $this->humanReadableDateFormat($this->options['format']);
120  $message = str_replace('%format', $humanReadableDateFormat, $message);
121  return $message;
122  }
123 
133  protected function humanReadableDateFormat($format)
134  {
135  $label = self::LOCALISATION_OBJECT_NAME . '.strftime.';
136  $pairs = [
137  '%A' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'A', 'form'),
138  '%a' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'a', 'form'),
139  '%d' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'd', 'form'),
140  '%e' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'e', 'form'),
141  '%B' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'B', 'form'),
142  '%b' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'b', 'form'),
143  '%m' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'm', 'form'),
144  '%Y' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'Y', 'form'),
145  '%y' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'y', 'form'),
146  '%H' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'H', 'form'),
147  '%I' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'I', 'form'),
148  '%M' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 'M', 'form'),
150  ];
151  $humanReadableFormat = str_replace(array_keys($pairs), array_values($pairs), $format);
152  return $humanReadableFormat;
153  }
154 }
static translate($key, $extensionName, $arguments=null)
addError($message, $code, array $arguments=[], $title='')
renderMessage($message=null, $type=null, $messageType='message')