newOptionAllowed not work when set it to false

Hi Team,

According to the documentation:

The newOptionAllowed attribute is used to enable adding new options.

As it is a boolean variable, so when it set to “false”, it should disable adding new options, but there is an issue when set it to “false”.

case 1:
set newOptionAllowed=false in xml to not allow new option, but set newOptionHandler to one of the method in controller. If user enters new value and press enter or lookupField loses focus, the handler will be called.

case 2:
set newOptionAllowed=false in xml to not allow new option, but in controller’s onInit() method, call setNewOptionHandler() of the lookupField to set a new option handler. The result is the same as case 1.

The problem here is, in both cases, newOptionAllowed is processed before newOptionHandler, but looking at the setNewOptionHandler method of lookupField, it does not care about how newOptionAllowed is set, but only check if handler is null:

    @Override
    public void setNewOptionHandler(Consumer<String> newOptionHandler) {
        this.newOptionHandler = newOptionHandler;

        if (newOptionHandler != null
                && component.getNewItemHandler() == null) {
            component.setNewItemHandler(this::onNewItemEntered);
        }

        if (newOptionHandler == null
                && component.getNewItemHandler() != null) {
            component.setNewItemHandler(null);
        }
    }

I think we should consider newOptionAllowed settings in setNewOptionHandler(), please suggest.

Hello!

newOptionAllowed is deprecated attribute, see LookupField#setNewOptionAllowed().

The setNewOptionHandler() method is a replacement for this feature. It means if you set a handler, it will be invoked despite newOptionAllowed=false.
If adding new items should be disabled, you can set setNewOptionHandler(null) or add a custom condition to your handler.

I’ve created an issue in documentation repo: cuba-platform/documentation#787

1 Like