Working with cms_input_list trough FS API
Author: civca
Publication Date: 2/9/2023 12:35
Hello community
I'm having trouble working with the cms_input_list component through the API.
I figured out how to access the model and the options within the model, how to set a new selection, and everything seems to work, but the selected value can't be saved afterwards.
Specifically, I'm doing some migration, I'm accessing a datasource, and I need to set cms_input_list to a certain value.
I did like this:
targetDataset = targetContent.getDataset(targetEntity);
targetData = targetDataset.getFormData();
.
.
.
//filed name dtMandant
GomFormElement gomFormElement = targetData.getForm().findEditor("dtMandant");
OptionFactory factory = ((OptionFactoryProvider) gomFormElement).getOptionFactory();
OptionModel optionModel = factory.getOptionModel(this.getClientScriptContext().getUserService().getConnection().getBroker(),
language, false);
Option option = optionModel.getOption("faculty_02");
optionModel.setSelectedOption(option);
this.getClientScriptContext().logInfo("--- option after set " + optionModel.getSelectedOption());//writes --- option after set faculty_02
this.getClientScriptContext().logInfo("--- mandant after set " + targetData.get(language,
"dtMandant").get());//writes --- mandant after set []
//it seems as if some connection between the objects is lost somewhere and the selection is not remembered
//for other input components everything works as it should
.
.
.
//of course, after setting all the other fields, I save it all
targetDataset.setFormData(targetData);
targetDataset.save();
-
Author: mbergmann - 2/9/2023 17:37
Hi,
when dealing with option based input components, you can just set an „appropriate value“ to the FormField. E.g. if it’s based on String (like it is when using <entries>), the following should work:
targetDataset = targetContent.getDataset(targetEntity);
targetData = targetDataset.getFormData();
targetData.get(language, "dtMandant").set(Collections.singleton("faculty_02"));
//of course, after setting all the other fields, I save it all
targetDataset.setFormData(targetData);
targetDataset.save();For other cases, i.e. when the INPUT_LIST is based on database entries you could simply use a Collection of Datasets or Entities in the FormField‘s .set(…) method.
The same applies to INPUT_CHECKBOX, INPUT_RADIOBUTTON and INPUT_COMBOBOX by the way - for the latter two, you don’t need a Collection of course but just the value itself.
Hope this helps!
Michael
0 -
Author: civca - 2/13/2023 8:15
thank you man, you helped me a lot.
0
Vous devez vous connecter pour laisser un commentaire.
Commentaires
2 commentaires