Let's say we have the following domain class
package com.xyzcorp
class Widget {
Date dateCreated
String sku
int quantity = 0
static mapping = {
autoTimestamp true
}
}
Now imagine we have a controller where we are creating the widget and the dateCreated or quantity properties should never be set by the UI (since our system is maintaining those internally). Here is how:
// some code in our controller
Widget widget = new Widget()
bindData(widget, params, [
exclude: [
'dateCreated',
'quantity'
]
])
As you can see we are using bindData to bind the params presented to our controller to our widget instance while excluding the dateCreated and quantity properties.
You could argue that we should just set the sku property in the constructor, but on larger domain classes it can be easier to exclude properties instead. Plus, our intent is pretty clear for future developers who are maintaining our code that we don't ever want dateCreated or quantity to be set from the UI. A big plus if you ask me!
This is just an example of excluding data from binding and bindData has quite a few other options besides exclude, so I encourage you to explore them.
No comments:
Post a Comment