Make fields read-only based on the status of entity

I’m new to the CUBA Platform and I cannot find a way to make some fields read-only based on specific business logic.

Take the Workshop tutorial application for example. Say, I want to stop you from editing the Client, Mechanic and Hours Spent fields once the Status has been changed to “Ready”, but you should still be able to edit the Description field. If the order status is changed back to “In Progress” those fields should be editable again.

How would I achieve the above?

Thanks in advance

Hi,

You need to add ItemPropertyChangeListener to a data source and check the status of an Order.

@Inject
private OrderService orderService;
@Named("fieldGroup.client")
private PickerField clientField;
@Named("fieldGroup.mechanic")
private PickerField mechanicField;
@Named("fieldGroup.hoursSpent")
private TextField hoursSpentField;
@Inject
private Datasource<Order> orderDs;

@Override
public void init(Map<String, Object> params) {
    orderDs.addItemPropertyChangeListener(e -> {
        if ("status".equals(e.getProperty())) {
            updateFields();
        }
    });
}

private void updateFields() {
    boolean editable = !OrderStatus.READY.equals(getItem().getStatus());
    clientField.setEditable(editable);
    mechanicField.setEditable(editable);
    hoursSpentField.setEditable(editable);
}

@Override
protected void postInit() {
    updateFields();
}

Regards,
Gleb

1 Like

Thanks Gleb, this works great!