Views
8 months ago

tornadofx-guide

11. Editing Models and

11. Editing Models and Validation to bind a button to the valid state of the model you can call model.validate(decorateErrors = false) to force all validators to report their results without actually showing any validation errors to the user. field("username") { textfield(username).required() } field("password") { passwordfield(password).required() } buttonbar { button("Login", ButtonBar.ButtonData.OK_DONE) { enableWhen(model.valid) action { model.commit { doLogin() } } } } // Force validators to update the `model.valid` property model.validate(decorateErrors = false) Notice how the login button's enabled state is bound to the enabled state of the model via enableWhen { model.valid } call. After all the fields and validators are configured, the model.validate(decorateErrors = false) make sure the valid state of the model is updated without triggering error decorations on the fields that fail validation. The decorators will kick in on value change by default, unless you override the trigger parameter to validator . The required() build in validator also accepts this parameter. For example, to run the validator only when the input field looses focus you can call textfield(username).required(ValidationTrigger.OnBlur) . Validation in dialogs The dialog builder creates a window with a form and a fieldset and let's you start adding fields to it. Some times you don't have a ViewModel for such cases, but you might still want to use the features it provides. For such situations you can instantiate a ViewModel inline and hook up one or more properties to it. Here is an example dialog that requires the user to enter some input in a textarea: 172

11. Editing Models and Validation dialog("Add note") { val model = ViewModel() val note = model.bind { SimpleStringProperty() } } field("Note") { textarea(note) { required() whenDocked { requestFocus() } } } buttonbar { button("Save note").action { model.commit { doSave() } } } Figure 11.3 A dialog with a inline ViewModel context Notice how the note property is connected to the context by specifying it's bean parameter. This is crucial for making the field validation available. Partial commit It's also possible to do a partial commit by suppling a list of fields you want to commit to avoid committing everything. This can be convenient in situations where you edit the same ViewModel instance from different Views, for example in a Wizard. See the Wizard chapter 173

GUIDE
GUIDE
Guide
GUIDE
Guide
GUIDE
GUIDE
Guide
GUIDE
Guide
Guide
GUIDE
GUIDE
GUIDE
GUIDE