如何在grails域子类中继承属性添加约束 [英] How to add constraints on inherited properties in a grails domain sub-class

查看:180
本文介绍了如何在grails域子类中继承属性添加约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我想做的:

class A {
  String string
  static constraints = {
    string(maxSize:100)
  }
}

class B extends A {
  static constraints = {
    string(url:true)
  }
}

所以A类应该有

So class A should have some constraints and B should have the same plus additional constraints on the same property.

我有一些的约束,而且一些约束和B应该在同一个属性上有工作,我可以想象它会与每层次的表概念冲突。

I couldn't get that to work though and I can imagine that it would clash with the Table-per-Hierarchy concept.

所以我试图解决这个问题,引入一个Command对象与类B约束可以在类B的构造函数中验证。但是,似乎Command对象只能在控制器中使用(grails一直说没有.validate()方法)。

So I tried to work around that problem by introducing a Command object with class B's constraints which can be validated in the constructor of class B. However it seems that Command objects can only be used within controllers (grails keeps saying that there is no .validate() method for it).

所以我的问题是:使用grails约束解决这个问题最简单的方法是什么(不是手动重新实现验证)?

So my question is: What is the most elegant way to solve this using grails constraints (not re-implementing the validation manually)? Could be...


  • 切换到每个子类别的概念?


> Edit:我可以在子类中定义所有的约束,重复父类的约束,甚至没有父类中的约束。但是解决方案应该适用于同一父类的多个子类(具有不同的约束)。

It would be okay for me to define all the constraints in the child classes, repeating the constraints of the parent class or not even having constraints in the parent class at all. But the solution should work for multiple child classes (with different constraints) of the same parent class.

推荐答案

p>

You can use

    class B extends A {
       static constraints = {
          importFrom A
          //B stuff
       }
    }

作为http://grails.org/doc/latest/ref/Constraints/Usage.html

这篇关于如何在grails域子类中继承属性添加约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆