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