Reputation: 463
I have two domain classes in Grails that will interact with corresponding form fields for state and country. Is there any way to conditionally bind them so that a user can't make the mistake of trying to submit something wrong. For instance, "Chicago, IL" would be valid, but "Chicago, Mexico" would be invalid. Would it be easier to do this within the gsp or the controller? Thanks for the help - this is something I haven't attempted before.
class State {
String name
String value
int orderNumber = 0
static constraints = {
name nullable:false, maxSize:50, blank:false
value nullable:false, maxSize:100, blank:false
}
String toString(){
"$name - $value"
}
static mapping = {
table 'state'
cache: 'read-write'
columns{
id column:'id'
name column:'name' //abbreviation
value column:'value' //state name
orderNumber column:'order_number' // numerical list order
}
id generator: 'assigned'
}
}
class Country {
String name
String value
int orderNumber = 0
static constraints = {
name nullable:false, maxSize:50, blank:false
value nullable:false, maxSize:100, blank:false
}
String toString(){
"$name - $value"
}
static mapping = {
table 'country'
cache: 'read-write'
columns{
id column:'id'
name column:'name' //abbreviation
value column:'value' // country name
orderNumber column:'order_number' // numerical list order
}
id generator: 'assigned'
}
}
Form fields
<div class="col-sm-1">
State<g:select name="State" from="" value="" class="form-control" type="text" label="state" required="true"/>
</div>
<div class="col-sm-2">
Country<g:select name="Country" from="" class="form-control" type="text" label="country" required="true"/>
</div>
Upvotes: 0
Views: 346
Reputation: 1029
You will need to consume the form input into a command object, and have a custom validator on that command object with your validation logic. Plenty of good details in the docs.
Good luck!
Upvotes: 2