|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* 2017 Romain CANON <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the TYPO3 Formz project. |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
|
7
|
|
|
* under the terms of the GNU General Public License, either |
|
8
|
|
|
* version 3 of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, see: |
|
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Romm\Formz\AssetHandler\JavaScript; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\Formz\AssetHandler\AbstractAssetHandler; |
|
17
|
|
|
use Romm\Formz\Error\FormzMessageInterface; |
|
18
|
|
|
use Romm\Formz\Service\ArrayService; |
|
19
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Will handle the "one time run" data needed by JavaScript: the submitted |
|
23
|
|
|
* values, and others. |
|
24
|
|
|
*/ |
|
25
|
|
|
class FormRequestDataJavaScriptAssetHandler extends AbstractAssetHandler |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* See class description. |
|
30
|
|
|
* |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getFormRequestDataJavaScriptCode() |
|
34
|
|
|
{ |
|
35
|
|
|
$submittedFormValues = []; |
|
36
|
|
|
$fieldsExistingMessages = []; |
|
37
|
|
|
$deactivatedFields = []; |
|
38
|
|
|
$formWasSubmitted = $this->getFormObject()->formWasSubmitted() |
|
39
|
|
|
? 'true' |
|
40
|
|
|
: 'false'; |
|
41
|
|
|
|
|
42
|
|
|
if ($formWasSubmitted) { |
|
43
|
|
|
$submittedFormValues = ArrayService::get()->arrayToJavaScriptJson($this->getSubmittedFormValues()); |
|
44
|
|
|
$fieldsExistingMessages = ArrayService::get()->arrayToJavaScriptJson($this->getFieldsExistingMessages()); |
|
45
|
|
|
$deactivatedFields = ArrayService::get()->arrayToJavaScriptJson($this->getDeactivatedFields()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$formName = GeneralUtility::quoteJSvalue($this->getFormObject()->getName()); |
|
49
|
|
|
|
|
50
|
|
|
$javaScriptCode = <<<JS |
|
51
|
|
|
(function() { |
|
52
|
|
|
Formz.Form.beforeInitialization($formName, function(form) { |
|
53
|
|
|
form.injectRequestData($submittedFormValues, $fieldsExistingMessages, $formWasSubmitted, $deactivatedFields) |
|
54
|
|
|
}); |
|
55
|
|
|
})(); |
|
56
|
|
|
JS; |
|
57
|
|
|
|
|
58
|
|
|
return $javaScriptCode; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Will fetch the values of the fields of the submitted form, if a form has |
|
63
|
|
|
* been submitted. |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getSubmittedFormValues() |
|
68
|
|
|
{ |
|
69
|
|
|
$result = []; |
|
70
|
|
|
$formName = $this->getFormObject()->getName(); |
|
71
|
|
|
$originalRequest = $this->getControllerContext() |
|
72
|
|
|
->getRequest() |
|
73
|
|
|
->getOriginalRequest(); |
|
74
|
|
|
|
|
75
|
|
|
if (null !== $originalRequest |
|
76
|
|
|
&& $originalRequest->hasArgument($formName) |
|
77
|
|
|
) { |
|
78
|
|
|
$result = $originalRequest->getArgument($formName); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $result; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* This function checks every message which may exist on every property of |
|
86
|
|
|
* the form (used to tell to JavaScript which messages already exist). |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function getFieldsExistingMessages() |
|
91
|
|
|
{ |
|
92
|
|
|
$fieldsMessages = []; |
|
93
|
|
|
$formObject = $this->getFormObject(); |
|
94
|
|
|
|
|
95
|
|
|
if ($formObject->formWasSubmitted() |
|
96
|
|
|
&& $formObject->hasLastValidationResult() |
|
97
|
|
|
) { |
|
98
|
|
|
$lastValidationResult = $this->getFormObject()->getLastValidationResult(); |
|
99
|
|
|
|
|
100
|
|
|
foreach ($this->getFormObject()->getProperties() as $fieldName) { |
|
101
|
|
|
$result = $lastValidationResult->forProperty($fieldName); |
|
102
|
|
|
$messages = []; |
|
103
|
|
|
|
|
104
|
|
|
if ($result->hasErrors()) { |
|
105
|
|
|
$messages['errors'] = $this->formatMessages($result->getErrors()); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if ($result->hasWarnings()) { |
|
109
|
|
|
$messages['warnings'] = $this->formatMessages($result->getWarnings()); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if ($result->hasNotices()) { |
|
113
|
|
|
$messages['notices'] = $this->formatMessages($result->getNotices()); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$fieldsMessages[$fieldName] = $messages; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $fieldsMessages; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param FormzMessageInterface[] $messages |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function formatMessages(array $messages) |
|
128
|
|
|
{ |
|
129
|
|
|
$sortedMessages = []; |
|
130
|
|
|
|
|
131
|
|
|
foreach ($messages as $message) { |
|
132
|
|
|
$validationName = $message->getValidationName(); |
|
133
|
|
|
$messageKey = $message->getMessageKey(); |
|
134
|
|
|
|
|
135
|
|
|
if (false === isset($sortedMessages[$validationName])) { |
|
136
|
|
|
$sortedMessages[$validationName] = []; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$sortedMessages[$validationName][$messageKey] = $message->render(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $sortedMessages; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return array |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function getDeactivatedFields() |
|
149
|
|
|
{ |
|
150
|
|
|
return ($this->getFormObject()->hasLastValidationResult()) |
|
151
|
|
|
? array_keys($this->getFormObject()->getLastValidationResult()->getDeactivatedFields()) |
|
152
|
|
|
: []; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: