【问题标题】:Grails Command Object validation for input list输入列表的 Grails 命令对象验证
【发布时间】:2019-12-10 16:15:40
【问题描述】:

您好,如何从下面的 html 中为这 2 个列表 event[] 和 qty[] 设置命令对象约束为空、空白和自定义?

<div class="row-1">
    <select name="event[]" class="form-control ">
        <option selected="">abc</option>
        <option selected="">def</option>
    </select>
    <input name="qty[]" >
</div>
<div class="row-2">
    <select name="event[]" class="form-control ">
        <option selected="">ghi</option>
        <option selected="">jkl</option>
    </select>
    <input name="qty[]" >
</div>


class someCommand implements Validateable {

    List eventComponent
    List qty

    static constraints = {
    }
}

【问题讨论】:

    标签: grails grails-orm command-objects


    【解决方案1】:

    您好,我如何设置命令对象约束为空、空白和 这两个列表的自定义

    你可以这样做:

    class someCommand implements Validateable {
    
        List eventComponent
        List qty
    
        static constraints = {
            eventComponent nullable: false, validator: { theList ->
                // return true if theList is valid
                // return false or a message code if theList is invalid
            }
            qty nullable: false
        }
    }
    

    您不会将blank 用于Listblank 用于验证 String 属性。如果你想确保列表中有元素,你可以使用minSize: 1

    希望对你有帮助。

    【讨论】:

    • 我认为由于标记的构造方式,您将遇到不同的问题,但以上回答了有关如何为这些属性设置约束的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多