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\Html; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\AssetHandler\AbstractAssetHandler; |
17
|
|
|
use Romm\Formz\Error\FormResult; |
18
|
|
|
use Romm\Formz\Error\FormzMessageInterface; |
19
|
|
|
use Romm\Formz\Service\MessageService; |
20
|
|
|
use Romm\Formz\Service\StringService; |
21
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
22
|
|
|
use TYPO3\CMS\Extbase\Reflection\ObjectAccess; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* This asset handler generates several data attributes which will be added to |
26
|
|
|
* the form element in the Fluid template. Most of these data attributes are |
27
|
|
|
* directly bound to fields and their properties. |
28
|
|
|
* |
29
|
|
|
* Example of data attributes: |
30
|
|
|
* - Fields values: when a field changes, its new value will be indicated in |
31
|
|
|
* the form with the attribute: `fz-value-{field name}="value"`. |
32
|
|
|
* - Fields validation: when a field is considered as valid (it passed all its |
33
|
|
|
* validation rules), the form gets the attribute: `fz-valid-{field name}`. |
34
|
|
|
* - Fields errors: when a field validation fails with an error, the form gets |
35
|
|
|
* the attribute: `fz-error-{field name}-{name of the error}`. |
36
|
|
|
* - Fields warnings and notices: same as errors. |
37
|
|
|
*/ |
38
|
|
|
class DataAttributesAssetHandler extends AbstractAssetHandler |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Handles the data attributes containing the values of the form fields. |
43
|
|
|
* |
44
|
|
|
* Example: `fz-value-color="blue"` |
45
|
|
|
* |
46
|
|
|
* @param FormResult $formResult |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getFieldsValuesDataAttributes(FormResult $formResult) |
50
|
|
|
{ |
51
|
|
|
$result = []; |
52
|
|
|
$formObject = $this->getFormObject(); |
53
|
|
|
$formInstance = $formObject->getForm(); |
54
|
|
|
|
55
|
|
|
foreach ($formObject->getConfiguration()->getFields() as $field) { |
56
|
|
|
$fieldName = $field->getName(); |
57
|
|
|
|
58
|
|
|
if (false === $formResult->fieldIsDeactivated($field)) { |
59
|
|
|
$value = ObjectAccess::getProperty($formInstance, $fieldName); |
60
|
|
|
$value = (is_array($value)) |
61
|
|
|
? implode(' ', $value) |
62
|
|
|
: $value; |
63
|
|
|
|
64
|
|
|
if (false === empty($value)) { |
65
|
|
|
$result[self::getFieldDataValueKey($fieldName)] = $value; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Handles the data attributes for the fields which got errors, warnings and |
75
|
|
|
* notices. |
76
|
|
|
* |
77
|
|
|
* Examples: |
78
|
|
|
* - `fz-error-email="1"` |
79
|
|
|
* - `fz-error-email-rule-default="1"` |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function getFieldsMessagesDataAttributes() |
84
|
|
|
{ |
85
|
|
|
$result = []; |
86
|
|
|
$formConfiguration = $this->getFormObject()->getConfiguration(); |
87
|
|
|
$formResult = $this->getFormObject()->getFormResult(); |
88
|
|
|
|
89
|
|
|
foreach ($formResult->getSubResults() as $fieldName => $fieldResult) { |
90
|
|
|
if (true === $formConfiguration->hasField($fieldName) |
91
|
|
|
&& false === $formResult->fieldIsDeactivated($formConfiguration->getField($fieldName)) |
92
|
|
|
) { |
93
|
|
|
$result += $this->getFieldErrorMessages($fieldName, $fieldResult); |
94
|
|
|
$result += $this->getFieldWarningMessages($fieldName, $fieldResult); |
95
|
|
|
$result += $this->getFieldNoticeMessages($fieldName, $fieldResult); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $result; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $fieldName |
104
|
|
|
* @param Result $fieldResult |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
protected function getFieldErrorMessages($fieldName, Result $fieldResult) |
108
|
|
|
{ |
109
|
|
|
return (true === $fieldResult->hasErrors()) |
110
|
|
|
? $this->addFieldMessageDataAttribute($fieldName, $fieldResult->getErrors(), 'error') |
|
|
|
|
111
|
|
|
: []; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $fieldName |
116
|
|
|
* @param Result $fieldResult |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
protected function getFieldWarningMessages($fieldName, Result $fieldResult) |
120
|
|
|
{ |
121
|
|
|
return (true === $fieldResult->hasWarnings()) |
122
|
|
|
? $this->addFieldMessageDataAttribute($fieldName, $fieldResult->getWarnings(), 'warning') |
|
|
|
|
123
|
|
|
: []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $fieldName |
128
|
|
|
* @param Result $fieldResult |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
protected function getFieldNoticeMessages($fieldName, Result $fieldResult) |
132
|
|
|
{ |
133
|
|
|
return (true === $fieldResult->hasNotices()) |
134
|
|
|
? $this->addFieldMessageDataAttribute($fieldName, $fieldResult->getNotices(), 'notice') |
|
|
|
|
135
|
|
|
: []; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $fieldName |
140
|
|
|
* @param FormzMessageInterface[] $messages |
141
|
|
|
* @param string $type |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
protected function addFieldMessageDataAttribute($fieldName, array $messages, $type) |
145
|
|
|
{ |
146
|
|
|
$result = [self::getFieldDataMessageKey($fieldName, $type) => '1']; |
147
|
|
|
|
148
|
|
|
foreach ($messages as $message) { |
149
|
|
|
$validationName = MessageService::get()->getMessageValidationName($message); |
150
|
|
|
$messageKey = MessageService::get()->getMessageKey($message); |
151
|
|
|
|
152
|
|
|
$result[self::getFieldDataValidationMessageKey($fieldName, $type, $validationName, $messageKey)] = '1'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $result; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Handles the data attributes for the fields which are valid. |
160
|
|
|
* |
161
|
|
|
* Example: `fz-valid-email="1"` |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
public function getFieldsValidDataAttributes() |
166
|
|
|
{ |
167
|
|
|
$result = []; |
168
|
|
|
$formConfiguration = $this->getFormObject()->getConfiguration(); |
169
|
|
|
$formResult = $this->getFormObject()->getFormResult(); |
170
|
|
|
|
171
|
|
|
foreach ($formConfiguration->getFields() as $field) { |
172
|
|
|
$fieldName = $field->getName(); |
173
|
|
|
|
174
|
|
|
if (false === $formResult->fieldIsDeactivated($field) |
175
|
|
|
&& false === $formResult->forProperty($fieldName)->hasErrors() |
176
|
|
|
) { |
177
|
|
|
$result[self::getFieldDataValidKey($fieldName)] = '1'; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $result; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Formats the data value attribute key for a given field name. |
186
|
|
|
* |
187
|
|
|
* @param string $fieldName Name of the field. |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public static function getFieldDataValueKey($fieldName) |
191
|
|
|
{ |
192
|
|
|
return 'fz-value-' . StringService::get()->sanitizeString($fieldName); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Formats the data valid attribute key for a given field name. |
197
|
|
|
* |
198
|
|
|
* @param string $fieldName Name of the field. |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
public static function getFieldDataValidKey($fieldName) |
202
|
|
|
{ |
203
|
|
|
return 'fz-valid-' . StringService::get()->sanitizeString($fieldName); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Formats the data message attribute key for a given field name. |
208
|
|
|
* |
209
|
|
|
* @param string $fieldName Name of the field. |
210
|
|
|
* @param string $type Type of the message: `error`, `warning` or `notice`. |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
public static function getFieldDataMessageKey($fieldName, $type = 'error') |
214
|
|
|
{ |
215
|
|
|
return 'fz-' . $type . '-' . StringService::get()->sanitizeString($fieldName); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
public static function getFieldSubmissionDone() |
222
|
|
|
{ |
223
|
|
|
return 'fz-submission-done'; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Formats the data message attribute key for a given failed validation for |
228
|
|
|
* the given field name. |
229
|
|
|
* |
230
|
|
|
* @param string $fieldName |
231
|
|
|
* @param string $type Type of the message: `error`, `warning` or `notice`. |
232
|
|
|
* @param string $validationName |
233
|
|
|
* @param string $messageKey |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
public static function getFieldDataValidationMessageKey($fieldName, $type, $validationName, $messageKey) |
237
|
|
|
{ |
238
|
|
|
$stringService = StringService::get(); |
239
|
|
|
|
240
|
|
|
return vsprintf( |
241
|
|
|
'fz-%s-%s-%s-%s', |
242
|
|
|
[ |
243
|
|
|
$type, |
244
|
|
|
$stringService->sanitizeString($fieldName), |
245
|
|
|
$stringService->sanitizeString($validationName), |
246
|
|
|
$stringService->sanitizeString($messageKey) |
247
|
|
|
] |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
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: