‪TYPO3CMS  9.5
CreatableFormElementPropertiesValidator.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 
25 {
26 
40  public function ‪__invoke(string $key, $value)
41  {
42  $dto = $this->validationDto->withPropertyPath($key);
43 
44  if ($this->‪getConfigurationService()->isFormElementPropertyDefinedInFormEditorSetup($dto)) {
45  if ($this->‪getConfigurationService()->formElementPropertyHasLimitedAllowedValuesDefinedWithinFormEditorSetup($dto)) {
46  $this->‪validateFormElementValue($value, $dto);
47  }
48  } elseif (
49  $this->‪getConfigurationService()->isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup($dto)
50  && !‪ArrayUtility::isValidPath($this->currentElement, $this->‪buildHmacDataPath($dto->getPropertyPath()), '.')
51  ) {
53  } else {
55  $this->currentElement,
56  $value,
57  $this->sessionToken,
58  $dto
59  );
60  }
61  }
62 
72  $value,
73  ‪ValidationDto $dto
74  ): void {
75  // If the form element is newely created, we have to compare the $value (form definition) with $predefinedDefaultValue (form setup)
76  // to check the integrity (at this time we don't have a hmac for the $value to check the integrity)
78  if ($value !== $predefinedDefaultValue) {
79  $throwException = true;
80 
81  if (is_string($predefinedDefaultValue)) {
82  // Last chance:
83  // Get all translations (from all backend languages) for the untranslated! $predefinedDefaultValue and
84  // compare the (already translated) $value (from the form definition) against the possible
85  // translations from $predefinedDefaultValue.
86  // Usecase:
87  // * backend language is EN
88  // * open the form edtior and add a ContentElement form element
89  // * switch to another browser tab and change the backend language to DE
90  // * clear the cache
91  // * go back to the form editor and click the save button
92  // Out of scope:
93  // * the same scenario as above + delete the previous chosen backend language within the maintenance tool
94  $untranslatedPredefinedDefaultValue = $this->‪getConfigurationService()->‪getFormElementPredefinedDefaultValueFromFormEditorSetup($dto, false);
96  $untranslatedPredefinedDefaultValue,
97  $dto->‪getPrototypeName()
98  );
99 
100  if (in_array($value, $translations, true)) {
101  $throwException = false;
102  }
103  }
104 
105  if ($throwException) {
106  $message = 'The value "%s" of property "%s" (form element "%s") is not equal to the default value "%s" #1528588035';
107  throw new ‪PropertyException(
108  sprintf(
109  $message,
110  $value,
111  $dto->‪getPropertyPath(),
113  $predefinedDefaultValue
114  ),
115  1528588035
116  );
117  }
118  }
119  }
120 
129  protected function ‪validateFormElementValue(
130  $value,
131  ‪ValidationDto $dto
132  ): void {
134 
135  if (!in_array($value, $allowedValues, true)) {
136  $untranslatedAllowedValues = $this->‪getConfigurationService()->‪getAllowedValuesForFormElementPropertyFromFormEditorSetup($dto, false);
137  // Compare the $value against the untranslated set of allowed values
138  if (in_array($value, $untranslatedAllowedValues, true)) {
139  // All good, $value is within the untranslated set of allowed values
140  return;
141  }
142  // Get all translations (from all backend languages) for the untranslated! $allowedValues and
143  // compare the (already translated) $value (from the form definition) against all possible
144  // translations for $untranslatedAllowedValues.
145  $allPossibleAllowedValuesTranslations = $this->‪getConfigurationService()->‪getAllBackendTranslationsForTranslationKeys(
146  $untranslatedAllowedValues,
147  $dto->‪getPrototypeName()
148  );
149 
150  foreach ($allPossibleAllowedValuesTranslations as $translations) {
151  if (in_array($value, $translations, true)) {
152  // All good, $value is within the set of translated allowed values
153  return;
154  }
155  }
156 
157  // Last chance:
158  // If $value is not configured within the form setup as an allowed value
159  // but was written within the form definition by hand (and therefore contains a hmac),
160  // check if $value is manipulated.
161  // If $value has no hmac or if the hmac exists but is not valid,
162  // then $this->validatePropertyCollectionElementPropertyValueByHmacData() will
163  // throw an exception.
165  $this->currentElement,
166  $value,
167  $this->sessionToken,
168  $dto
169  );
170  }
171  }
172 }
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidator\__invoke
‪__invoke(string $key, $value)
Definition: CreatableFormElementPropertiesValidator.php:40
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ValidationDto\getPrototypeName
‪string getPrototypeName()
Definition: ValidationDto.php:76
‪TYPO3\CMS\Extbase\Property\Exception
Definition: DuplicateObjectException.php:2
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ValidationDto
Definition: ValidationDto.php:23
‪TYPO3\CMS\Core\Utility\ArrayUtility\isValidPath
‪static bool isValidPath(array $array, $path, $delimiter='/')
Definition: ArrayUtility.php:143
‪TYPO3\CMS\Form\Domain\Configuration\ConfigurationService\getAllBackendTranslationsForTranslationKeys
‪array getAllBackendTranslationsForTranslationKeys(array $keys, string $prototypeName)
Definition: ConfigurationService.php:523
‪TYPO3\CMS\Form\Domain\Configuration\ConfigurationService\getFormElementPredefinedDefaultValueFromFormEditorSetup
‪mixed getFormElementPredefinedDefaultValueFromFormEditorSetup(ValidationDto $dto, bool $translated=true)
Definition: ConfigurationService.php:380
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\AbstractValidator\buildHmacDataPath
‪string buildHmacDataPath(string $propertyPath)
Definition: AbstractValidator.php:63
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ValidationDto\getPropertyPath
‪string getPropertyPath()
Definition: ValidationDto.php:100
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ElementBasedValidator
Definition: ElementBasedValidator.php:25
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidator
Definition: CreatableFormElementPropertiesValidator.php:25
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ValidationDto\getFormElementIdentifier
‪string getFormElementIdentifier()
Definition: ValidationDto.php:92
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidator\validateFormElementValue
‪validateFormElementValue( $value, ValidationDto $dto)
Definition: CreatableFormElementPropertiesValidator.php:129
‪TYPO3\CMS\Form\Domain\Configuration\ConfigurationService\getAllowedValuesForFormElementPropertyFromFormEditorSetup
‪array getAllowedValuesForFormElementPropertyFromFormEditorSetup(ValidationDto $dto, bool $translated=true)
Definition: ConfigurationService.php:220
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators
Definition: AbstractValidator.php:3
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\AbstractValidator\getConfigurationService
‪ConfigurationService getConfigurationService()
Definition: AbstractValidator.php:83
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ElementBasedValidator\validateFormElementPropertyValueByHmacData
‪validateFormElementPropertyValueByHmacData(array $currentElement, $value, string $sessionToken, ValidationDto $dto)
Definition: ElementBasedValidator.php:38
‪TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException
Definition: PropertyException.php:24
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidator\validateFormElementPredefinedDefaultValue
‪validateFormElementPredefinedDefaultValue( $value, ValidationDto $dto)
Definition: CreatableFormElementPropertiesValidator.php:71
‪TYPO3\CMS\Form\Domain\Configuration\ConfigurationService\getAllBackendTranslationsForTranslationKey
‪array getAllBackendTranslationsForTranslationKey(string $key, string $prototypeName)
Definition: ConfigurationService.php:542