Grails:如何在域类属性上设置元约束?

发布于 2024-10-14 08:31:54 字数 715 浏览 7 评论 0原文

我有一个属于订阅的联系人类,我想对订阅属性设置一个假设的只读约束,以便在脚手架模板中使用。

该类看起来像是

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尹雨沫 2024-10-21 08:31:54

在脚手架模板中,有一个来自 org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass 类型的属性domainClass。这些对象具有属性 constrainedProperties。要超出“只读”范围,您必须执行以下操作:

您的域类:

class Contact {
   static belongsTo = [subscription: Subscription]

   static constraints = {
       subscription(nullable: false, attributes: [readonly: true])  
   }

   String description
}

在脚手架模板中:

def ro = domainClass.constrainedProperties.subscription.attributes.readonly

DefaultGrailsDomainClass 有一个带有 Class 类型属性的构造函数,也许您可​​以这样做:

def domainClass = new DefaultGrailsDomainClass(Contact.class)
def ro = domainClass.constrainedProperties.subscription.attributes.readonly

也许有一个用于此目的的工厂,但我不知道。

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:

class Contact {
   static belongsTo = [subscription: Subscription]

   static constraints = {
       subscription(nullable: false, attributes: [readonly: true])  
   }

   String description
}

in the scaffolding template:

def ro = domainClass.constrainedProperties.subscription.attributes.readonly

the DefaultGrailsDomainClass has a constructor with a attribute from type Class maybe you can do this:

def domainClass = new DefaultGrailsDomainClass(Contact.class)
def ro = domainClass.constrainedProperties.subscription.attributes.readonly

Maybe there is a Factory for this, but I don't know.

羁绊已千年 2024-10-21 08:31:54

如果您特别想要一个影响脚手架表单字段的只读约束,您可以使用:

static constraints = {
    subscription(editable: false)
}

这是 renderEditor.template 使用的约束列表(我可以找到无论如何,快速搜索):

  • 可编辑(如果 false,导致呈现的字段为只读 - 适用于字符串和日期字段)
  • 小部件(如果“textarea”,字段呈现为文本区域 - 适用于字符串字段)
  • 格式(对于日期字段,向 datePicker 的 format 属性提供约束值)

If you specifically want a readonly constraint that influences the scaffolded form fields, you can use:

static constraints = {
    subscription(editable: false)
}

Here's a list of the constraints that are used by renderEditor.template (that I could find with a quick search, anyway):

  • editable (if false, causes rendered field to be readonly - works for String and Date fields)
  • widget (if 'textarea', field is rendered as a textarea - works for String fields)
  • format (for date fields, supplies the constraint value to the datePicker's format attribute)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文