我在Grails中有两个域类,它们将与州和国家的相应表单字段进行交互。有没有办法有条件地绑定它们,以便用户不会犯错误尝试提交错误。例如,“芝加哥,IL”将是有效的,但“芝加哥,墨西哥”将无效。在gsp或控制器中执行此操作会更容易吗?感谢您的帮助 - 这是我以前没有尝过的。
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'
}
}
表单字段
<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>