diff --git a/assets/controllers/elements/structural_entity_select_controller.js b/assets/controllers/elements/structural_entity_select_controller.js index 38480cfa..93d26d01 100644 --- a/assets/controllers/elements/structural_entity_select_controller.js +++ b/assets/controllers/elements/structural_entity_select_controller.js @@ -57,10 +57,29 @@ export default class extends Controller { '(' + addHint +')' + ''; }, - } + }, + + //Add callbacks to update validity + onInitialize: this.updateValidity.bind(this), + onChange: this.updateValidity.bind(this), }; this._tomSelect = new TomSelect(this.element, settings); + this._tomSelect.sync(); + } + + + updateValidity() { + //Mark this input as invalid, if the selected option is disabled + + const input = this.element; + const selectedOption = input.options[input.selectedIndex]; + + if (selectedOption && selectedOption.disabled) { + input.setCustomValidity("This option was disabled. Please select another option."); + } else { + input.setCustomValidity(""); + } } getTomSelect() {