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