TYPO3  7.6
ValidationBuilder.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Form\Domain\Builder;
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(ValidationBuilder::class);
35  $validationBuilder->setConfiguration($configuration);
36  return $validationBuilder;
37  }
38 
42  protected $rules = array();
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 = array())
111  {
112  $userConfiguredFormTyposcript = $this->configuration->getTypoScript();
113  $rulesTyposcript = isset($userConfiguredFormTyposcript['rules.']) ? $userConfiguredFormTyposcript['rules.'] : null;
114  $this->rules[$this->configuration->getPrefix()] = array();
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'] = array($ruleArguments['error.'], $ruleArguments['error']);
132  $keysToRemove = array_flip(array(
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  $mandatoryMessage = $validator->renderMessage($ruleArguments['message.'], $ruleArguments['message']);
151 
152  $this->rules[$this->configuration->getPrefix()][$fieldName][] = array(
153  'validator' => $validator,
154  'validatorName' => $validatorClassName,
155  'validatorOptions' => $validatorOptions,
156  'mandatoryMessage' => $mandatoryMessage
157  );
158  } else {
159  throw new \RuntimeException('Class "' . $validatorClassName . '" could not be loaded.');
160  }
161  }
162  }
163  }
164  }
165 
172  public function setRules(array $rules)
173  {
174  $this->rules = $rules[$this->configuration->getPrefix()];
175  }
176 
182  public function getRules()
183  {
184  return $this->rules[$this->configuration->getPrefix()];
185  }
186 
194  public function setRulesByElementName($key = '', array $rule = array())
195  {
196  $this->rules[$this->configuration->getPrefix()][$key] = $rule;
197  }
198 
205  public function getRulesByElementName($key = '')
206  {
207  if (isset($this->rules[$this->configuration->getPrefix()][$key])) {
208  return $this->rules[$this->configuration->getPrefix()][$key];
209  }
210  return null;
211  }
212 
219  public function removeRule($key = '')
220  {
221  unset($this->rules[$this->configuration->getPrefix()][$key]);
222  }
223 
231  {
232  $mandatoryMessages = array();
233  if ($this->getRulesByElementName($key)) {
234  $rules = $this->getRulesByElementName($key);
235  foreach ($rules as $rule) {
236  $mandatoryMessages[] = $rule['mandatoryMessage'];
237  }
238  }
239  return $mandatoryMessages;
240  }
241 }