41
41
use Nextras \FormComponents \Controls \DateTimeControl ;
42
42
use stdClass ;
43
43
use Throwable ;
44
+ use function array_key_exists ;
44
45
use function array_slice ;
45
46
use function array_values ;
46
47
use function explode ;
@@ -130,11 +131,12 @@ public function createComponentForm() : Form
130
131
$ form = $ this ->baseFormFactory ->create ();
131
132
132
133
foreach ($ this ->customInputRepository ->findByRolesOrderedByPosition ($ this ->user ->getRoles ()) as $ customInput ) {
133
- $ custom = null ;
134
+ $ customInputId = 'custom ' . $ customInput ->getId ();
135
+ $ custom = null ;
134
136
135
137
switch (true ) {
136
138
case $ customInput instanceof CustomText:
137
- $ custom = $ form ->addText (' custom ' . $ customInput -> getId () , $ customInput ->getName ());
139
+ $ custom = $ form ->addText ($ customInputId , $ customInput ->getName ());
138
140
139
141
/** @var ?CustomTextValue $customInputValue */
140
142
$ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
@@ -145,7 +147,7 @@ public function createComponentForm() : Form
145
147
break ;
146
148
147
149
case $ customInput instanceof CustomCheckbox:
148
- $ custom = $ form ->addCheckbox (' custom ' . $ customInput -> getId () , $ customInput ->getName ());
150
+ $ custom = $ form ->addCheckbox ($ customInputId , $ customInput ->getName ());
149
151
150
152
/** @var ?CustomCheckboxValue $customInputValue */
151
153
$ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
@@ -156,18 +158,19 @@ public function createComponentForm() : Form
156
158
break ;
157
159
158
160
case $ customInput instanceof CustomSelect:
159
- $ custom = $ form ->addSelect ('custom ' . $ customInput ->getId (), $ customInput ->getName (), $ customInput ->getSelectOptions ());
161
+ $ selectOptions = $ customInput ->getSelectOptions ();
162
+ $ custom = $ form ->addSelect ($ customInputId , $ customInput ->getName (), $ selectOptions );
160
163
161
164
/** @var ?CustomSelectValue $customInputValue */
162
165
$ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
163
- if ($ customInputValue ) {
166
+ if ($ customInputValue && array_key_exists ( $ customInputValue -> getValue (), $ selectOptions ) ) {
164
167
$ custom ->setDefaultValue ($ customInputValue ->getValue ());
165
168
}
166
169
167
170
break ;
168
171
169
172
case $ customInput instanceof CustomMultiSelect:
170
- $ custom = $ form ->addMultiSelect (' custom ' . $ customInput -> getId () , $ customInput ->getName (), $ customInput ->getSelectOptions ());
173
+ $ custom = $ form ->addMultiSelect ($ customInputId , $ customInput ->getName (), $ customInput ->getSelectOptions ());
171
174
172
175
/** @var ?CustomMultiSelectValue $customInputValue */
173
176
$ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
@@ -178,7 +181,7 @@ public function createComponentForm() : Form
178
181
break ;
179
182
180
183
case $ customInput instanceof CustomFile:
181
- $ custom = $ form ->addUpload (' custom ' . $ customInput -> getId () , $ customInput ->getName ());
184
+ $ custom = $ form ->addUpload ($ customInputId , $ customInput ->getName ());
182
185
183
186
/** @var ?CustomFileValue $customInputValue */
184
187
$ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
@@ -198,7 +201,7 @@ public function createComponentForm() : Form
198
201
$ custom ->setDefaultValue ($ customInputValue ->getValue ());
199
202
}
200
203
201
- $ form ->addComponent ($ custom , ' custom ' . $ customInput -> getId () );
204
+ $ form ->addComponent ($ custom , $ customInputId );
202
205
break ;
203
206
204
207
case $ customInput instanceof CustomDateTime:
@@ -210,7 +213,7 @@ public function createComponentForm() : Form
210
213
$ custom ->setDefaultValue ($ customInputValue ->getValue ());
211
214
}
212
215
213
- $ form ->addComponent ($ custom , ' custom ' . $ customInput -> getId () );
216
+ $ form ->addComponent ($ custom , $ customInputId );
214
217
break ;
215
218
216
219
default :
@@ -249,41 +252,37 @@ public function processForm(Form $form, stdClass $values) : void
249
252
250
253
if ($ this ->applicationService ->isAllowedEditCustomInputs ()) {
251
254
foreach ($ this ->customInputRepository ->findByRolesOrderedByPosition ($ this ->user ->getRoles ()) as $ customInput ) {
255
+ $ customInputId = 'custom ' . $ customInput ->getId ();
252
256
$ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
253
- $ customInputName = 'custom ' . $ customInput ->getId ();
254
257
$ oldValue = null ;
255
- $ newValue = null ;
258
+ $ newValue = $ values -> $ customInputId ;
256
259
257
260
if ($ customInput instanceof CustomText) {
258
261
/** @var CustomTextValue $customInputValue */
259
262
$ customInputValue = $ customInputValue ?: new CustomTextValue ();
260
263
$ oldValue = $ customInputValue ->getValue ();
261
- $ newValue = $ values ->$ customInputName ;
262
264
$ customInputValue ->setValue ($ newValue );
263
265
} elseif ($ customInput instanceof CustomCheckbox) {
264
266
/** @var CustomCheckboxValue $customInputValue */
265
267
$ customInputValue = $ customInputValue ?: new CustomCheckboxValue ();
266
268
$ oldValue = $ customInputValue ->getValue ();
267
- $ newValue = $ values ->$ customInputName ;
268
269
$ customInputValue ->setValue ($ newValue );
269
270
} elseif ($ customInput instanceof CustomSelect) {
270
271
/** @var CustomSelectValue $customInputValue */
271
272
$ customInputValue = $ customInputValue ?: new CustomSelectValue ();
272
273
$ oldValue = $ customInputValue ->getValue ();
273
- $ newValue = $ values ->$ customInputName ;
274
274
$ customInputValue ->setValue ($ newValue );
275
275
} elseif ($ customInput instanceof CustomMultiSelect) {
276
276
/** @var CustomMultiSelectValue $customInputValue */
277
277
$ customInputValue = $ customInputValue ?: new CustomMultiSelectValue ();
278
278
$ oldValue = $ customInputValue ->getValue ();
279
- $ newValue = $ values ->$ customInputName ;
280
279
$ customInputValue ->setValue ($ newValue );
281
280
} elseif ($ customInput instanceof CustomFile) {
282
281
/** @var CustomFileValue $customInputValue */
283
282
$ customInputValue = $ customInputValue ?: new CustomFileValue ();
284
283
$ oldValue = $ customInputValue ->getValue ();
285
284
/** @var FileUpload $newValue */
286
- $ newValue = $ values ->$ customInputName ;
285
+ $ newValue = $ values ->$ customInputId ;
287
286
if ($ newValue ->getError () == UPLOAD_ERR_OK ) {
288
287
$ path = $ this ->generatePath ($ newValue );
289
288
$ this ->filesService ->save ($ newValue , $ path );
@@ -293,13 +292,11 @@ public function processForm(Form $form, stdClass $values) : void
293
292
/** @var CustomDateValue $customInputValue */
294
293
$ customInputValue = $ customInputValue ?: new CustomDateValue ();
295
294
$ oldValue = $ customInputValue ->getValue ();
296
- $ newValue = $ values ->$ customInputName ;
297
295
$ customInputValue ->setValue ($ newValue );
298
296
} elseif ($ customInput instanceof CustomDateTime) {
299
297
/** @var CustomDateTimeValue $customInputValue */
300
298
$ customInputValue = $ customInputValue ?: new CustomDateTimeValue ();
301
299
$ oldValue = $ customInputValue ->getValue ();
302
- $ newValue = $ values ->$ customInputName ;
303
300
$ customInputValue ->setValue ($ newValue );
304
301
}
305
302
0 commit comments