2

カスタム制約を作成しようとしています。ロジックをサービスに入れました:

class RegExpManagerService {

    boolean transactional = false
    def messageSource

    def lookupRegexp(regExpression,Locale locale) {

       def pattern = messageSource.getMessage( regExpression,null,locale )
       return pattern
    }

    def testRegexp(regExpression,text,Locale locale) {
       return text ==~ lookupRegexp(regExpression,locale)
    }
}

ドメインコントローラーに挿入しようとしました:

class Tag extends IbidemBaseDomain {

    def regExpManagerService
    static hasMany=[itemTags:ItemTag]
    static mapping = {
        itemTags fetch:"join"
    }

    //Long id
    Date dateCreated
    Date lastUpdated
    String tag
    // Relation
    Tagtype tagtype
    // Relation
    Customer customer
    // Relation
    Person updatedByPerson
    // Relation
    Person createdByPerson

    static constraints = {
        dateCreated(nullable: true)
        lastUpdated(nullable: true)
        tag(blank: false,validator: {val,obj ->
                regExpManagerService.testRegexp(obj.tagtype.regexpression,val,local)
        })
        tagtype(nullable: true)
        customer(nullable: true)
        updatedByPerson(nullable: true)
        createdByPerson(nullable: true)
    }
    String toString() {
        return "${tag}" 
    }
}

制約が実行されると、次のエラーが発生します。

2009-08-24 18:50:53,562 [http-8080-1] ERROR errors.GrailsExceptionResolver  - groovy.lang.MissingPropertyException: No such property: regExpManagerService for class: org.maflt.ibidem.Tag
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingPropertyException: No such property: regExpManagerService for class: org.maflt.ibidem.Tag
4

1 に答える 1

3

制約クロージャーは静的であるため、インスタンス フィールド 'regExpManagerService' を認識できません。ただし、オブジェクトが検証されているため、そこからアクセスできます。

   tag(blank: false,validator: {val,obj ->
      obj.regExpManagerService.testRegexp(obj.tagtype.regexpression,val,local)
   })
于 2009-08-25T02:10:35.060 に答える