TYPO3 CMS  TYPO3_7-6
ValidationBuilder.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 
21 
26 {
31  public static function create(Configuration $configuration)
32  {
34  $validationBuilder = \TYPO3\CMS\Form\Utility\FormUtility::getObjectManager()->get(self::class);
35  $validationBuilder->setConfiguration($configuration);
36  return $validationBuilder;
37  }
38 
42  protected $rules = [];
43 
47  protected $formPrefix = '';
48 
52  protected $objectManager;
53 
58 
62  protected $formUtility;
63 
67  protected $configuration;
68 
73  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
74  {
75  $this->objectManager = $objectManager;
76  }
77 
82  public function injectTypoScriptRepository(\TYPO3\CMS\Form\Domain\Repository\TypoScriptRepository $typoScriptRepository)
83  {
84  $this->typoScriptRepository = $typoScriptRepository;
85  }
86 
90  public function setConfiguration(Configuration $configuration)
91  {
92  $this->configuration = $configuration;
93  }
94 
99  {
100  $this->formUtility = $formUtility;
101  }
102 
110  public function buildRules(array $rawArgument = [])
111  {
112  $userConfiguredFormTyposcript = $this->configuration->getTypoScript();
113  $rulesTyposcript = isset($userConfiguredFormTyposcript['rules.']) ? $userConfiguredFormTyposcript['rules.'] : null;
114  $this->rules[$this->configuration->getPrefix()] = [];
115  if (is_array($rulesTyposcript)) {
116  $keys = TemplateService::sortedKeyList($rulesTyposcript);
117  foreach ($keys as $key) {
118  $ruleName = $rulesTyposcript[$key];
119  $validatorClassName = $this->typoScriptRepository->getRegisteredClassName($ruleName, 'registeredValidators');
120  if ($validatorClassName === null) {
121  throw new \RuntimeException('Class "' . $validatorClassName . '" not registered via typoscript.');
122  }
123  if (
124  (int)$key
125  && strpos($key, '.') === false
126  ) {
127  $ruleArguments = $rulesTyposcript[$key . '.'];
128  $fieldName = $this->formUtility->sanitizeNameAttribute($ruleArguments['element']);
129  // remove unsupported validator options
130  $validatorOptions = $ruleArguments;
131  $validatorOptions['errorMessage'] = [$ruleArguments['error.'], $ruleArguments['error']];
132  $keysToRemove = array_flip([
133  'breakOnError',
134  'message',
135  'message.',
136  'error',
137  'error.',
138  'showMessage',
139  ]);
140  $validatorOptions = array_diff_key($validatorOptions, $keysToRemove);
141 
142  // Instantiate the validator to check if all required options are assigned
143  // and to use the validator message rendering function to pre-render the mandatory message
145  $validator = $this->objectManager->get($validatorClassName, $validatorOptions);
146 
147  if ($validator instanceof AbstractValidator) {
148  $validator->setRawArgument($rawArgument);
149  $validator->setFormUtility($this->formUtility);
150 
151  if ((int)$ruleArguments['showMessage'] === 1) {
152  $mandatoryMessage = $validator->renderMessage($ruleArguments['message.'], $ruleArguments['message']);
153  } else {
154  $mandatoryMessage = null;
155  }
156 
157  $this->rules[$this->configuration->getPrefix()][$fieldName][] = [
158  'validator' => $validator,
159  'validatorName' => $validatorClassName,
160  'validatorOptions' => $validatorOptions,
161  'mandatoryMessage' => $mandatoryMessage
162  ];
163  } else {
164  throw new \RuntimeException('Class "' . $validatorClassName . '" could not be loaded.');
165  }
166  }
167  }
168  }
169  }
170 
177  public function setRules(array $rules)
178  {
179  $this->rules = $rules[$this->configuration->getPrefix()];
180  }
181 
187  public function getRules()
188  {
189  return $this->rules[$this->configuration->getPrefix()];
190  }
191 
199  public function setRulesByElementName($key = '', array $rule = [])
200  {
201  $this->rules[$this->configuration->getPrefix()][$key] = $rule;
202  }
203 
210  public function getRulesByElementName($key = '')
211  {
212  if (isset($this->rules[$this->configuration->getPrefix()][$key])) {
213  return $this->rules[$this->configuration->getPrefix()][$key];
214  }
215  return null;
216  }
217 
224  public function removeRule($key = '')
225  {
226  unset($this->rules[$this->configuration->getPrefix()][$key]);
227  }
228 
236  {
237  $mandatoryMessages = [];
238  if ($this->getRulesByElementName($key)) {
239  $rules = $this->getRulesByElementName($key);
240  foreach ($rules as $rule) {
241  if ($rule['mandatoryMessage']) {
242  $mandatoryMessages[] = $rule['mandatoryMessage'];
243  }
244  }
245  }
246  return $mandatoryMessages;
247  }
248 }
static sortedKeyList($setupArr, $acceptOnlyProperties=false)
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
injectTypoScriptRepository(\TYPO3\CMS\Form\Domain\Repository\TypoScriptRepository $typoScriptRepository)