Getting data from a form

Hello and thank you for your constant support!

this time, I am trying to get some data from a form,
How do I get the data from a Form, If I am generating the form dynamically?

I am using this example:
https://doc.cuba-platform.com/manual-latest/gui_data_comp_prog.html

this is the kind of code I am using:

TextField stringTextField = uiComponents.create(TextField.TYPE_STRING);
stringTextField.setId(“text1”);
stringTextField.setCaption(“Entry 1”);

    TextField<String> stringTextField2 = uiComponents.create(TextField.TYPE_STRING);
    stringTextField2.setId("text2");
    stringTextField2.setCaption("Entry 2");
    
    form.add(stringTextField);
    form.add(stringTextField2);

Overall the form looks good, but I can not get the data from it in order to persist it. Are there any available examples?

Im new here…

//loop through components you added to the form
for (Component component : form.getComponents()) {
            //here you can get the component id
            component.getId();
             // and here you can get the component value
            ((Field) component).getValue().toString();
}

Is this a proper solution if you don’t have an entity attached to persist?

Hello @arturoams!

You need to bind the data container with the component. Then, when changing the value in the component, the entity will also change:

public class NewScreen extends Screen {

    @Inject
    protected DataComponents dataComponents;
    @Inject
    protected UiComponents uiComponents;

    protected InstanceContainer<TestEntity> testEntityDc;
    protected InstanceLoader<TestEntity> testEntityDl;

    @Subscribe
    protected void onInit(InitEvent event) {
        createDataComponents();
        createUiComponents();
    }

    protected void createDataComponents() {
        DataContext dataContext = dataComponents.createDataContext();
        getScreenData().setDataContext(dataContext);

        testEntityDc = dataComponents.createInstanceContainer(TestEntity.class);
        TestEntity testEntity = dataContext.create(TestEntity.class);
        testEntityDc.setItem(testEntity);

        testEntityDl = dataComponents.createInstanceLoader();
        testEntityDl.setContainer(testEntityDc);
        testEntityDl.setDataContext(dataContext);
    }

    protected void createUiComponents() {
        TextField<String> stringTextField = uiComponents.create(TextField.TYPE_STRING);
        stringTextField.setId("text1");
        stringTextField.setCaption("Entry 1");
        stringTextField.setValueSource(new ContainerValueSource<>(testEntityDc, "entry1"));

        TextField<String> stringTextField2 = uiComponents.create(TextField.TYPE_STRING);
        stringTextField2.setId("text2");
        stringTextField2.setCaption("Entry 2");
        stringTextField2.setValueSource(new ContainerValueSource<>(testEntityDc, "entry2"));

        Form form = uiComponents.create(Form.class);
        form.setId("form");
        form.add(stringTextField);
        form.add(stringTextField2);

        getWindow().add(form);
    }

    @Subscribe("saveBtn")
    protected void onSaveBtnClick(Button.ClickEvent event) {
        getScreenData().getDataContext().commit();
    }
}

If you want to get component value without data binding you can use the follow example:

TextField<String> stringTextField = uiComponents.create(TextField.TYPE_STRING);
stringTextField.setId("text1");
stringTextField.setCaption("Entry 1");
stringTextField.setValue("Test value");

Form form = uiComponents.create(Form.class);
form.setId("form");
form.add(stringTextField);

// method will return a string "Test value"
String componentValue = (String) ((WebTextField) form.getComponent("text1")).getValue();

Regards,
Gleb

1 Like