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