09.04.2018 Views

tornadofx-guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

11. Editing Models and Validation<br />

dialog("Add note") {<br />

val model = ViewModel()<br />

val note = model.bind { SimpleStringProperty() }<br />

}<br />

field("Note") {<br />

textarea(note) {<br />

required()<br />

whenDocked { requestFocus() }<br />

}<br />

}<br />

buttonbar {<br />

button("Save note").action {<br />

model.commit { doSave() }<br />

}<br />

}<br />

Figure 11.3 A dialog with a inline ViewModel context<br />

Notice how the note property is connected to the context by specifying it's bean parameter.<br />

This is crucial for making the field validation available.<br />

Partial commit<br />

It's also possible to do a partial commit by suppling a list of fields you want to commit to<br />

avoid committing everything. This can be convenient in situations where you edit the same<br />

ViewModel instance from different Views, for example in a Wizard. See the Wizard chapter<br />

173

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!