Kotlin DSL 2 field validation in `Configurable`
已回答
Hi, I'm writing a plugin that requires configuring a path for a language server. I want to add validation to the settings menu when the user changes the path value.
I have tried many things without success. The way I managed to do it is to imperatively change attributes of a predefined label depending on a validation function that runs with `onApply`
private val gleamPathSelect = GleamPathSelect()
private fun runPathValidation() {
if (!gleamPathSelect.isEnabled) return
LOG.warn("Running path validation")
val path = gleamPathSelect.text
val valid = FsUtils.validateGleamPath(path)
if (!valid) {
errorLabel.foreground = JBColor.RED
errorLabel.text = "Invalid path"
} else {
errorLabel.foreground = JBColor.GREEN
errorLabel.text = "Valid path"
}
}
/* ... */
override fun Panel.createContent() {
row(GleamBundle.message("gleam.settings.lsp.configurable.lsp.path")) {
/* private val gleamPathSelect = GleamPathSelect() */
cell(gleamPathSelect)
.align(AlignX.FILL)
// .errorOnApply("Gleam path is invalid") { true }
.onApply { runPathValidation() }
.bindText(settings::lspPath)
}
I would like to hook into the existing validation system, but I cannot seem to make it work. I looked at the Kotlin DSL UI demo source code but the validation section only covers live input validation. I have tried with apply, but even then ideally I would prefer to do the validation whenever the value has changed, in a debounced fashion.
Can someone give me some guidance?
请先登录再写评论。
Hi,
Try creating a validator and passing it to
validationOnApply
andvalidationOnInput
, for example (I'm not sure whatGleamPathSelect
, so showingtextField
):