Grails: how to set meta restriction on a property of a domain class?

I have a Contact class that belongs to a subscription, and I want to set a hypothetical readonly restriction on the subscription property that will be consumed in scaffold templates.

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 the ConstrainedProperty.addMetaConstraint method, which "adds meta-restrictions, which are an invalid informational restriction."

How do I call it from the Domain class?

And how do I get a meta restriction?

+4
source share
2 answers

Scaffolding templates have a domainClass property of type org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass. This object has the constrainedProperties property. To have excess for "readonly", you must do this:

Your domain class:

 class Contact { static belongsTo = [subscription: Subscription] static constraints = { subscription(nullable: false, attributes: [readonly: true]) } String description } 

in the forest template:

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

DefaultGrailsDomainClass has a constructor with an attribute of type Class, maybe you can do this:

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

Perhaps there is a Factory for this, but I do not know.

+3
source

If you specifically want the readonly constraint to affect the form fields of the forests, you can use:

 static constraints = { subscription(editable: false) } 

Here is a list of the limitations that renderEditor.template uses (which I could find in a quick search, anyway):

  • editable (if false , makes the displayed field be read-only - works for the String and Date fields)
  • (if "textarea", the field is displayed as a text field - works for string fields)
  • (for date fields, the binding value is assigned to the datePicker attribute)
+1
source

Source: https://habr.com/ru/post/1337000/


All Articles