繁体   English   中英

Grails:如何使用条件绑定域类

[英]Grails: how do bind domain classes with condition

我在Grails中有两个域类,它们将与州和国家/地区的相应表单字段进行交互。 有什么方法可以有条件地绑定它们,以使用户不会犯尝试提交错误的错误。 例如,“伊利诺伊州芝加哥”将是有效的,而“墨西哥芝加哥”将是无效的。 在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>

您将需要使用输入到命令对象中的表单,并使用验证逻辑在该命令对象上具有自定义验证器 文档中有很多不错的细节。

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM