edvin / tornadofx-guide

TornadoFX Guide
133 stars 67 forks source link

what is the tornadofx way to have constraints for properties? #116

Open ysm-coder opened 4 years ago

ysm-coder commented 4 years ago

I couldnt find it in the guide, but bassicaly if I have some constraints (like String should only contain lowercase letters) I would check them in the setters and if something is wrong I can throw an Exception, display an Alert, return from the setter without setting anything or whatever. But in tornadofx I dont have var+setters but instead I have val properties like SimpleStringProperty. So what should I do? One idea is to somehow check this in .onChange{} but the problems I see with it is that this happens after the change (what if someone else also listens to the change and does something before I can check the constraint, also how do I get my old value back?). What is the intended way to do this?

edvin commented 4 years ago

This is nicely solved in the validation support inside of the form builder. Please see Validation section of the guide, on this page: https://github.com/edvin/tornadofx-guide/blob/master/part1/11.%20Editing%20Models%20and%20Validation.md