4141use Nextras \FormComponents \Controls \DateTimeControl ;
4242use stdClass ;
4343use Throwable ;
44+ use function array_key_exists ;
4445use function array_slice ;
4546use function array_values ;
4647use function explode ;
@@ -130,11 +131,12 @@ public function createComponentForm() : Form
130131 $ form = $ this ->baseFormFactory ->create ();
131132
132133 foreach ($ this ->customInputRepository ->findByRolesOrderedByPosition ($ this ->user ->getRoles ()) as $ customInput ) {
133- $ custom = null ;
134+ $ customInputId = 'custom ' . $ customInput ->getId ();
135+ $ custom = null ;
134136
135137 switch (true ) {
136138 case $ customInput instanceof CustomText:
137- $ custom = $ form ->addText (' custom ' . $ customInput -> getId () , $ customInput ->getName ());
139+ $ custom = $ form ->addText ($ customInputId , $ customInput ->getName ());
138140
139141 /** @var ?CustomTextValue $customInputValue */
140142 $ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
@@ -145,7 +147,7 @@ public function createComponentForm() : Form
145147 break ;
146148
147149 case $ customInput instanceof CustomCheckbox:
148- $ custom = $ form ->addCheckbox (' custom ' . $ customInput -> getId () , $ customInput ->getName ());
150+ $ custom = $ form ->addCheckbox ($ customInputId , $ customInput ->getName ());
149151
150152 /** @var ?CustomCheckboxValue $customInputValue */
151153 $ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
@@ -156,18 +158,19 @@ public function createComponentForm() : Form
156158 break ;
157159
158160 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 );
160163
161164 /** @var ?CustomSelectValue $customInputValue */
162165 $ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
163- if ($ customInputValue ) {
166+ if ($ customInputValue && array_key_exists ( $ customInputValue -> getValue (), $ selectOptions ) ) {
164167 $ custom ->setDefaultValue ($ customInputValue ->getValue ());
165168 }
166169
167170 break ;
168171
169172 case $ customInput instanceof CustomMultiSelect:
170- $ custom = $ form ->addMultiSelect (' custom ' . $ customInput -> getId () , $ customInput ->getName (), $ customInput ->getSelectOptions ());
173+ $ custom = $ form ->addMultiSelect ($ customInputId , $ customInput ->getName (), $ customInput ->getSelectOptions ());
171174
172175 /** @var ?CustomMultiSelectValue $customInputValue */
173176 $ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
@@ -178,7 +181,7 @@ public function createComponentForm() : Form
178181 break ;
179182
180183 case $ customInput instanceof CustomFile:
181- $ custom = $ form ->addUpload (' custom ' . $ customInput -> getId () , $ customInput ->getName ());
184+ $ custom = $ form ->addUpload ($ customInputId , $ customInput ->getName ());
182185
183186 /** @var ?CustomFileValue $customInputValue */
184187 $ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
@@ -198,7 +201,7 @@ public function createComponentForm() : Form
198201 $ custom ->setDefaultValue ($ customInputValue ->getValue ());
199202 }
200203
201- $ form ->addComponent ($ custom , ' custom ' . $ customInput -> getId () );
204+ $ form ->addComponent ($ custom , $ customInputId );
202205 break ;
203206
204207 case $ customInput instanceof CustomDateTime:
@@ -210,7 +213,7 @@ public function createComponentForm() : Form
210213 $ custom ->setDefaultValue ($ customInputValue ->getValue ());
211214 }
212215
213- $ form ->addComponent ($ custom , ' custom ' . $ customInput -> getId () );
216+ $ form ->addComponent ($ custom , $ customInputId );
214217 break ;
215218
216219 default :
@@ -249,41 +252,37 @@ public function processForm(Form $form, stdClass $values) : void
249252
250253 if ($ this ->applicationService ->isAllowedEditCustomInputs ()) {
251254 foreach ($ this ->customInputRepository ->findByRolesOrderedByPosition ($ this ->user ->getRoles ()) as $ customInput ) {
255+ $ customInputId = 'custom ' . $ customInput ->getId ();
252256 $ customInputValue = $ this ->user ->getCustomInputValue ($ customInput );
253- $ customInputName = 'custom ' . $ customInput ->getId ();
254257 $ oldValue = null ;
255- $ newValue = null ;
258+ $ newValue = $ values -> $ customInputId ;
256259
257260 if ($ customInput instanceof CustomText) {
258261 /** @var CustomTextValue $customInputValue */
259262 $ customInputValue = $ customInputValue ?: new CustomTextValue ();
260263 $ oldValue = $ customInputValue ->getValue ();
261- $ newValue = $ values ->$ customInputName ;
262264 $ customInputValue ->setValue ($ newValue );
263265 } elseif ($ customInput instanceof CustomCheckbox) {
264266 /** @var CustomCheckboxValue $customInputValue */
265267 $ customInputValue = $ customInputValue ?: new CustomCheckboxValue ();
266268 $ oldValue = $ customInputValue ->getValue ();
267- $ newValue = $ values ->$ customInputName ;
268269 $ customInputValue ->setValue ($ newValue );
269270 } elseif ($ customInput instanceof CustomSelect) {
270271 /** @var CustomSelectValue $customInputValue */
271272 $ customInputValue = $ customInputValue ?: new CustomSelectValue ();
272273 $ oldValue = $ customInputValue ->getValue ();
273- $ newValue = $ values ->$ customInputName ;
274274 $ customInputValue ->setValue ($ newValue );
275275 } elseif ($ customInput instanceof CustomMultiSelect) {
276276 /** @var CustomMultiSelectValue $customInputValue */
277277 $ customInputValue = $ customInputValue ?: new CustomMultiSelectValue ();
278278 $ oldValue = $ customInputValue ->getValue ();
279- $ newValue = $ values ->$ customInputName ;
280279 $ customInputValue ->setValue ($ newValue );
281280 } elseif ($ customInput instanceof CustomFile) {
282281 /** @var CustomFileValue $customInputValue */
283282 $ customInputValue = $ customInputValue ?: new CustomFileValue ();
284283 $ oldValue = $ customInputValue ->getValue ();
285284 /** @var FileUpload $newValue */
286- $ newValue = $ values ->$ customInputName ;
285+ $ newValue = $ values ->$ customInputId ;
287286 if ($ newValue ->getError () == UPLOAD_ERR_OK ) {
288287 $ path = $ this ->generatePath ($ newValue );
289288 $ this ->filesService ->save ($ newValue , $ path );
@@ -293,13 +292,11 @@ public function processForm(Form $form, stdClass $values) : void
293292 /** @var CustomDateValue $customInputValue */
294293 $ customInputValue = $ customInputValue ?: new CustomDateValue ();
295294 $ oldValue = $ customInputValue ->getValue ();
296- $ newValue = $ values ->$ customInputName ;
297295 $ customInputValue ->setValue ($ newValue );
298296 } elseif ($ customInput instanceof CustomDateTime) {
299297 /** @var CustomDateTimeValue $customInputValue */
300298 $ customInputValue = $ customInputValue ?: new CustomDateTimeValue ();
301299 $ oldValue = $ customInputValue ->getValue ();
302- $ newValue = $ values ->$ customInputName ;
303300 $ customInputValue ->setValue ($ newValue );
304301 }
305302
0 commit comments