TornadoFX:比较 ValidationContext 中的 2 个表单值
TornadoFX: Compare 2 form values in ValidationContext
在 tornadofx 中,我试图验证表单中的两个输入值是否相等。我遵循了 this 指南,一切都按预期进行。但是我遇到了我无法检查输入中的两个值是否相等的方法。
例如,假设我想创建一个简单的注册表单,我必须在其中检查两个密码是否相等。我试过的是:
val validator = ValidationContext()
validator.addValidator(this, this.textProperty()) {
if(!password!!.isEqualTo(it).get()) //password1 != password2 -> does not work
error("Passwords do not equal")
}
我查看了 login example 希望能在示例代码中找到帮助,但没有成功。
有没有办法比较验证上下文中的输入?如果是这样怎么办?
编辑: 这确实有效,但我认为这不是在验证上下文中检查输入的理想方式。有没有更好的办法?
if (password.get() != password2.get())
error("Passwords do not equal") //Returns the error message
您可以为每个字段创建验证器,以便它们与其他字段进行比较。然后,您需要确保在更改字段时重新评估其他字段的验证器。确保包含 focusFirstError = false
以避免在输入字段中进行更改时焦点发生变化。
class DualValidationForm : View() {
private val vm = object : ViewModel() {
val text1Property = bind { SimpleStringProperty() }
val text2Property = bind { SimpleStringProperty() }
}
override val root = form {
fieldset("Make sure both fields have the same value") {
field("Text 1") {
textfield(vm.text1Property) {
validator {
if (it == vm.text2Property.value) null else ValidationMessage("Not the same!", ValidationSeverity.Error)
}
vm.text1Property.onChange {
vm.validate(focusFirstError = false, fields = vm.text2Property)
}
}
}
field("Text 2") {
textfield(vm.text2Property) {
validator {
if (it == vm.text1Property.value) null else ValidationMessage("Not the same!", ValidationSeverity.Error)
}
vm.text2Property.onChange {
vm.validate(focusFirstError = false, fields = vm.text1Property)
}
}
}
button("You can click me when both fields have the same value") {
enableWhen(vm.valid)
action {
information("Yay!", "You made it!").show()
}
}
}
}
}
在 tornadofx 中,我试图验证表单中的两个输入值是否相等。我遵循了 this 指南,一切都按预期进行。但是我遇到了我无法检查输入中的两个值是否相等的方法。
例如,假设我想创建一个简单的注册表单,我必须在其中检查两个密码是否相等。我试过的是:
val validator = ValidationContext()
validator.addValidator(this, this.textProperty()) {
if(!password!!.isEqualTo(it).get()) //password1 != password2 -> does not work
error("Passwords do not equal")
}
我查看了 login example 希望能在示例代码中找到帮助,但没有成功。
有没有办法比较验证上下文中的输入?如果是这样怎么办?
编辑: 这确实有效,但我认为这不是在验证上下文中检查输入的理想方式。有没有更好的办法?
if (password.get() != password2.get())
error("Passwords do not equal") //Returns the error message
您可以为每个字段创建验证器,以便它们与其他字段进行比较。然后,您需要确保在更改字段时重新评估其他字段的验证器。确保包含 focusFirstError = false
以避免在输入字段中进行更改时焦点发生变化。
class DualValidationForm : View() {
private val vm = object : ViewModel() {
val text1Property = bind { SimpleStringProperty() }
val text2Property = bind { SimpleStringProperty() }
}
override val root = form {
fieldset("Make sure both fields have the same value") {
field("Text 1") {
textfield(vm.text1Property) {
validator {
if (it == vm.text2Property.value) null else ValidationMessage("Not the same!", ValidationSeverity.Error)
}
vm.text1Property.onChange {
vm.validate(focusFirstError = false, fields = vm.text2Property)
}
}
}
field("Text 2") {
textfield(vm.text2Property) {
validator {
if (it == vm.text1Property.value) null else ValidationMessage("Not the same!", ValidationSeverity.Error)
}
vm.text2Property.onChange {
vm.validate(focusFirstError = false, fields = vm.text1Property)
}
}
}
button("You can click me when both fields have the same value") {
enableWhen(vm.valid)
action {
information("Yay!", "You made it!").show()
}
}
}
}
}