【问题标题】:Grails: how do bind domain classes with conditionGrails:如何将域类与条件绑定
【发布时间】:2014-08-08 19:49:02
【问题描述】:

我在 Grails 中有两个域类,它们将与州和国家/地区的相应表单字段进行交互。有没有办法有条件地绑定它们,这样用户就不会犯错误,试图提交错误的东西。例如,“Chicago, IL”是有效的,但“Chicago, Mexico”是无效的。在 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>

【问题讨论】:

  • 看看 ajaxdependancyselection 插件,它试图回答这类问题,我想还有几个类似的

标签: grails grails-domain-class


【解决方案1】:

您需要将表单输入使用到command object,并在该命令对象上使用您的验证逻辑有一个custom validator。文档中有很多很好的细节。

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2022-11-02
    相关资源
    最近更新 更多