[BUGFIX] Keep existing validation errors for recursive domain relations
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / CollectionValidator.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Validation\Validator;
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
17 /**
18 * A generic collection validator.
19 *
20 * @api
21 */
22 class CollectionValidator extends GenericObjectValidator
23 {
24 /**
25 * @var array
26 */
27 protected $supportedOptions = [
28 'elementValidator' => [null, 'The validator type to use for the collection elements', 'string'],
29 'elementType' => [null, 'The type of the elements in the collection', 'string'],
30 'validationGroups' => [null, 'The validation groups to link to', 'string'],
31 ];
32
33 /**
34 * @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver
35 */
36 protected $validatorResolver;
37
38 /**
39 * @param \TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver
40 */
41 public function injectValidatorResolver(\TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver)
42 {
43 $this->validatorResolver = $validatorResolver;
44 }
45
46 /**
47 * Checks if the given value is valid according to the validator, and returns
48 * the Error Messages object which occurred.
49 *
50 * @param mixed $value The value that should be validated
51 * @return \TYPO3\CMS\Extbase\Error\Result
52 * @api
53 */
54 public function validate($value)
55 {
56 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
57
58 if ($this->acceptsEmptyValues === false || $this->isEmpty($value) === false) {
59 if ((is_object($value) && !\TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::isCollectionType(get_class($value))) && !is_array($value)) {
60 $this->addError('The given subject was not a collection.', 1317204797);
61 return $this->result;
62 }
63 if ($value instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage && !$value->isInitialized()) {
64 return $this->result;
65 }
66 if (is_object($value)) {
67 if ($this->isValidatedAlready($value)) {
68 return $this->result;
69 }
70 $this->markInstanceAsValidated($value);
71 }
72 $this->isValid($value);
73 }
74 return $this->result;
75 }
76
77 /**
78 * Checks for a collection and if needed validates the items in the collection.
79 * This is done with the specified element validator or a validator based on
80 * the given element type and validation group.
81 *
82 * Either elementValidator or elementType must be given, otherwise validation
83 * will be skipped.
84 *
85 * @param mixed $value A collection to be validated
86 */
87 protected function isValid($value)
88 {
89 foreach ($value as $index => $collectionElement) {
90 if (isset($this->options['elementValidator'])) {
91 $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']);
92 } elseif (isset($this->options['elementType'])) {
93 if (isset($this->options['validationGroups'])) {
94 $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType'], $this->options['validationGroups']);
95 } else {
96 $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']);
97 }
98 } else {
99 return;
100 }
101 if ($collectionElementValidator instanceof ObjectValidatorInterface) {
102 $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer);
103 }
104 $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement));
105 }
106 }
107 }