Skip to content

Instantly share code, notes, and snippets.

@xlson
Created January 17, 2011 15:29
Show Gist options
  • Save xlson/782966 to your computer and use it in GitHub Desktop.
Save xlson/782966 to your computer and use it in GitHub Desktop.
Example of using the Grails dataBind method to change values of a command object after it has already been created
package testcommandobjects
class TestController {
static final int VALID = 5
static final int INVALID = 50
def resetToValid = { TestCommand cmd ->
rebindAgeAndRenderResponse(cmd, true)
}
def resetToInvalid = { TestCommand cmd ->
rebindAgeAndRenderResponse(cmd, false)
}
def gatherCommandData(cmd){
[value: cmd as String,
validates: cmd.validate()]
}
def rebindAgeAndRenderResponse(cmd, boolean changeToValid) {
def value = changeToValid ? VALID : INVALID
def before = gatherCommandData(cmd)
bindData(cmd, [age: value])
def after = gatherCommandData(cmd)
render buildOutput(before, after)
}
def buildOutput(before, after) {
def writer = new StringWriter()
def builder = new groovy.xml.MarkupBuilder(writer)
builder.html {
head {
title 'dataBind demo'
}
body {
h1 'dataBind demo'
h3 'valid data for age: 5..10'
p "$before"
p "$after"
}
}
writer.toString()
}
}
class TestCommand {
static constraints = {
age(range: 5..10)
}
int age
String toString() {
"age:[$age]"
}
}
@yan-kisen
Copy link

I think you mean "bindData" not "dataBind" :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment