Created
January 17, 2011 15:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you mean "bindData" not "dataBind" :)