From e68827bf3b4c7cd65f3d4cbd272fcc3ef488acce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Tue, 6 Jun 2023 23:05:44 +0200 Subject: [PATCH] Show a validation error message, when try to submit a form where a input is still set to a disabled value. Normally this would just send a null to the server, which often cause excptions. We now catch that earlier, and say the user that he have to select another option, when he tries to submit --- .../structural_entity_select_controller.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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() {