Grails:如何在域类属性上设置元约束?
我有一个属于订阅的联系人类,我想对订阅属性设置一个假设的只读约束,以便在脚手架模板中使用。
该类看起来像是
class Contact {
static belongsTo = [subscription: Subscription]
static constraints = {
subscription(nullable: false, readonly: true) // hypothetic *readonly* constraint
name(blank: false)
email(blank: false, email: true)
}
Integer id
String name
String email
String description
}
我找到的 ConstrainedProperty.addMetaConstraint 方法“添加元约束,这是非验证信息约束”。
如何从 Domain 类中调用它?
我如何获得元约束?
I have a Contact class that belongs to a Subscription and I want to set a hypothetic readonly constraint on the subscription property, to be consumed in the scaffold templates.
The class looks like
class Contact {
static belongsTo = [subscription: Subscription]
static constraints = {
subscription(nullable: false, readonly: true) // hypothetic *readonly* constraint
name(blank: false)
email(blank: false, email: true)
}
Integer id
String name
String email
String description
}
I found ConstrainedProperty.addMetaConstraint method that "adds a meta constraints which is a non-validating informational constraint".
How do I call it from within the Domain class?
And how do I get the meta-constraint?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在脚手架模板中,有一个来自 org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass 类型的属性domainClass。这些对象具有属性 constrainedProperties。要超出“只读”范围,您必须执行以下操作:
您的域类:
在脚手架模板中:
DefaultGrailsDomainClass 有一个带有 Class 类型属性的构造函数,也许您可以这样做:
也许有一个用于此目的的工厂,但我不知道。
In the scaffolding templates there is a property domainClass from type org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass. These object has the property constrainedProperties. To have excess to the 'readonly' you have to do this:
Your domain class:
in the scaffolding template:
the DefaultGrailsDomainClass has a constructor with a attribute from type Class maybe you can do this:
Maybe there is a Factory for this, but I don't know.
如果您特别想要一个影响脚手架表单字段的只读约束,您可以使用:
这是
renderEditor.template
使用的约束列表(我可以找到无论如何,快速搜索):false
,导致呈现的字段为只读 - 适用于字符串和日期字段)If you specifically want a
readonly
constraint that influences the scaffolded form fields, you can use:Here's a list of the constraints that are used by
renderEditor.template
(that I could find with a quick search, anyway):false
, causes rendered field to be readonly - works for String and Date fields)