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
This commit is contained in:
Jan Böhmer 2023-06-06 23:05:44 +02:00
parent 58bf69882f
commit e68827bf3b

View file

@ -57,10 +57,29 @@ export default class extends Controller {
'<small class="text-muted float-end">(' + addHint +')</small>' +
'</div>';
},
}
},
//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() {