【问题标题】:Make inherited constraints stricter使继承的约束更严格
【发布时间】:2016-10-02 09:09:30
【问题描述】:

在 Grails 中使用 CommandObjectsDomainClass 时如何限制继承属性的约束?

假设我有一个具有非空属性payload 的父类:

abstract class TextContentCommand extends ContentCommand {

    String payload

    static constraints = {            
        payload nullable: false
    }

在子类中我想让属性更严格并设置最大长度:

class FacebookTextContentCommand extends TextContentCommand {

    public static final int LENGTH_MAX = 4

    static constraints = {
        importFrom TextContentCommand
        payload maxSize: LENGTH_MAX
    }
}

这种方式不起作用,当提供更长的字符串时,验证通过。我对 Grails 的了解非常肤浅。如何限制继承的属性?

【问题讨论】:

    标签: grails grails-constraints


    【解决方案1】:

    常量LENGTH_MAX 导致了问题。直接提供值会使验证再次工作。

    class FacebookTextContentCommand extends TextContentCommand {
    
        static constraints = {
            importFrom TextContentCommand
            payload maxSize: 4
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-19
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      相关资源
      最近更新 更多