How to do a Read-only form (edit screen)

I need to show some Entity in a Edit screen in READ-ONLY mode, if certain conditions are met.
How do I open an entity in read-only mode or switch to it?
Thank you.

I usually do it in the onAfterShow() handler: setReadOnly(true). It’s that easy. :slight_smile:

1 Like

OK, thank you Jon @modemmisuser.
I figured this out too.
But this works only partially. This actually disables only the UI form elements, not the data source.

And it is not enough because the lookup fields still works in edit mode, the buttons witch sets data also works and the Close/Save dialog keeps asking if I want to save the data…

Is there a way set the data to read-only mode?

Thank you.

I tried window / data readOnly atribute.
But it doesnt work…
And it is not possible to set it programatically.

image

To prevent saving any programmatically modified data, in addition to calling setReadOnly(true) override the hasUnsavedChanges() method and define the PreSaveEvent handler as follows:

@Override
public boolean hasUnsavedChanges() {
    return isReadOnly() ? false : super.hasUnsavedChanges();
}

@Subscribe(target = Target.DATA_CONTEXT)
public void onPreSave(final DataContext.PreSaveEvent event) {
    if (isReadOnly())
        event.preventSave();
}
1 Like

OK, thank you, but I need it for JMix 1.5.
onPreSave() is for JMix 2.x.
@krivopustov is it possible?
Thank you.

In Jmix 1.5 use PreCommitEvent handler.