简体   繁体   English

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

[英]Grails: how to set a meta-constraint on domain class property?

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. 我有一个属于Subscription的Contact类,我想在订阅属性上设置一个hypothetic readonly约束,在scaffold模板中使用。

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". 我发现ConstrainedProperty.addMetaConstraint方法“添加了一个元限制,这是一种非验证信息约束”。

How do I call it from within the Domain class? 如何在Domain类中调用它?

And how do I get the meta-constraint? 我如何获得元约束?

In the scaffolding templates there is a property domainClass from type org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass. 在脚手架模板中,存在类型为org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass的属性domainClass。 These object has the property constrainedProperties. 这些对象具有属性constrainedProperties。 To have excess to the 'readonly' you have to do this: 要超出'readonly',你必须这样做:

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: DefaultGrailsDomainClass有一个带有类型Class属性的构造函数,也许你可以这样做:

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. 也许有一个工厂,但我不知道。

If you specifically want a readonly constraint that influences the scaffolded form fields, you can use: 如果您特别需要影响scaffolded表单字段的readonly约束,则可以使用:

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): 这是renderEditor.template使用的约束列表(无论如何我可以通过快速搜索找到):

  • editable (if false , causes rendered field to be readonly - works for String and Date fields) editable(如果为false ,导致呈现的字段为readonly - 适用于String和Date字段)
  • widget (if 'textarea', field is rendered as a textarea - works for String fields) 小部件(如果'textarea',字段呈现为textarea - 适用于String字段)
  • format (for date fields, supplies the constraint value to the datePicker's format attribute) format(对于日期字段,将约束值提供给datePicker的format属性)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM