Clear entityPicker programatical in UI

Hi,
I’m quite new to jmix and currently struggeling with following:

I have basic User entity and OtherEntity. OtherEntity has a reference to User with entityPicker in UI. After selecting the User I use onUserFieldComponentValueChange-Event to check, whether the selected user is active. If not, I want to clear the field in UI.
I’ve tried to clear it with following options:

userField.clear()
userField.setValue(null)
getEditedEntity().setUserID(null)

In detailView the entityPickerField changes to the selected user. And the field is never cleared in detailView. But I need it to be cleared in detailView, so that the users of the program can see it is empty. Back in ListView it shows, that an inactive user will not be saved. Is this a possible bug, or am I missing something?

I have added the classes of a small sample project for your reference.
entitypicker.zip (23.2 KB)
entityPicker_xml.zip (5.3 KB)

I’m using Jmix 2.3.1

Edit: if I would use entityComboBox and set itemsContainer the same code works as expected.

Many thanks,
Stefanie

Hi, Stefanie. Could you send arhive with whole project? Your first arhive is with compiled classes only and the second one contains only screens descriptors.

Thanks, Pavel.

Hi Pavel,
where/how can I upload files bigger than 4 MB? The whole project as zip is 8,5 MB.
Regards,
Stefanie

As an option you can publish it on GitHub. It would be usefull.

Hi Pavel,
it is now published on github
Regards,
Stefanie

Hi, Stefanie! There is some ways to solve your problem.

Technically the best solution is to use selectValidator of lookup action.

    @Install(to = "userField.entityLookup", subject = "selectValidator")
    private boolean userFieldEntityLookupSelectValidator(final LookupView.ValidationContext<User> validationContext) {
        return validationContext.getSelectedItems().iterator().next().getActive();
    }

Another way is to use validator of EntityPicker

    @Install(to = "userField", subject = "validator")
    private void userFieldValidator(final User value) {
        if(value != null && !value.getActive()){
           throw  new ValidationException("User is not active");
        }
    }

Also you can filter values in lookup window(there are different ways to perform it) to prevent wrong values selection.

Additionally there are options of using entity lifecycle event listeners or ItemPropertyChangeEvent. The way to change value of editinig entity is not correct logically(we need prevend without entity changing) and changing value of editing entity of ComponentValueChangeEvent is not recommended.

Best wishes, Pavel.

Hi Pavel,
I’ve tried this options and they work great!
Thanks a lot for your help and time :slight_smile:
All the best,
Stefanie

1 Like

I’m very glad that the problem have been solved.

Best wishes!
Pavel.