1
|
|
|
<?php |
2
|
|
|
namespace DERHANSEN\SfEventMgt\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
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Registration; |
18
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
19
|
|
|
use TYPO3\CMS\Extbase\Persistence\ObjectStorage; |
20
|
|
|
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* RegistrationFieldValidator |
24
|
|
|
* |
25
|
|
|
* @author Torben Hansen <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class RegistrationFieldValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Validates the additional fields of the given registration |
31
|
|
|
* |
32
|
|
|
* @param Registration $registration |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
protected function isValid($registration) |
36
|
|
|
{ |
37
|
|
|
$result = true; |
38
|
|
|
if ($registration->getFieldValues()->count() === 0 && |
39
|
|
|
$registration->getEvent()->getRegistrationFields()->count() === 0) { |
40
|
|
|
return $result; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @var Registration\Field $registrationField */ |
44
|
|
|
foreach ($registration->getEvent()->getRegistrationFields() as $registrationField) { |
45
|
|
|
$validationResult = $this->validateField($registrationField, $registration->getFieldValues()); |
46
|
|
|
if ($validationResult === false && $result === true) { |
47
|
|
|
$result = false; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
return $result; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Validates the given registrationField |
55
|
|
|
* |
56
|
|
|
* @param Registration\Field $registrationField |
57
|
|
|
* @param ObjectStorage $fieldValues |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
protected function validateField($registrationField, $fieldValues) |
61
|
|
|
{ |
62
|
|
|
$result = true; |
63
|
|
|
if (!$registrationField->getRequired()) { |
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @var NotEmptyValidator $validator */ |
68
|
|
|
$validator = $this->getNotEmptyValidator(); |
69
|
|
|
|
70
|
|
|
$fieldValue = $this->getFieldValue($registrationField, $fieldValues); |
71
|
|
|
$validationResult = $validator->validate($fieldValue); |
72
|
|
|
if ($validationResult->hasErrors()) { |
73
|
|
|
$result = false; |
74
|
|
|
foreach ($validationResult->getErrors() as $error) { |
75
|
|
|
$this->result->forProperty('fields.' . $registrationField->getUid())->addError($error); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns a notEmptyValidator |
83
|
|
|
* |
84
|
|
|
* @return NotEmptyValidator |
85
|
|
|
*/ |
86
|
|
|
protected function getNotEmptyValidator() |
87
|
|
|
{ |
88
|
|
|
return GeneralUtility::makeInstance(NotEmptyValidator::class); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the value for the given registrationField from the given fieldValues |
93
|
|
|
* |
94
|
|
|
* @param Registration\Field $registrationField |
95
|
|
|
* @param ObjectStorage $fieldValues |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
protected function getFieldValue($registrationField, $fieldValues) |
99
|
|
|
{ |
100
|
|
|
$result = ''; |
101
|
|
|
/** @var Registration\FieldValue $fieldValue */ |
102
|
|
|
foreach ($fieldValues as $fieldValue) { |
103
|
|
|
if ($fieldValue->getField()->getUid() === $registrationField->getUid()) { |
104
|
|
|
$result = $fieldValue->getValue(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
return $result; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|