2016-12-06 120 views
0

我想實現一個自定義的驗證器類。互聯網上有一些教程,例如http://blog.swwomm.com/2011/02/custom-grails-constraints.html在這些教程中描述,你必須在Config.groovy中註冊驗證類如何在Grails 3中註冊自定義的驗證器類

的問題是Config.groovy中通過的Grails 3. application.groovy我application.groovy看起來是這樣的替換:

import at.byte_code.businessSuite.core.NamespaceValidatorConstraint 
import grails.validation.ConstrainedProperty 

ConstrainedProperty.removeConstraint(ConstrainedProperty.VALIDATOR_CONSTRAINT) 
ConstrainedProperty.registerNewConstraint(ConstrainedProperty.VALIDATOR_CONSTRAINT, NamespaceValidatorConstraint.class) 

但是當我嘗試運行應用程序,我得到以下錯誤:

| Error Error occurred running Grails CLI: startup failed: 
script1481056327870569414787.groovy: 1: unable to resolve class at.byte_code.businessSuite.core.NamespaceValidatorConstraint 
@ line 1, column 1. 
    import at.byte_code.businessSuite.core.NamespaceValidatorConstraint 
^

script1481056327870569414787.groovy: 2: unable to resolve class grails.validation.ConstrainedProperty 
@ line 2, column 1. 
    import grails.validation.ConstrainedProperty 
^

如何註冊我的自定義驗證程序類?

回答

1

這是由於這樣的事實:在Grails的3,禁止進口第三方班application.groovy。要使用第三方類,請創建一個runtime.groovy文件。

+0

您可以鏈接任何描述此限制的文檔嗎? – Andreas

+0

http://docs.grails.org/3.2.7/guide/upgrading.html - 段落GORM 6配置模型 – Mikhail

0

一個解決方案(也許還有其他人)是該代碼添加到應用程序類本身:

class Application extends GrailsAutoConfiguration { 

    static void main(String[] args) { 
     GrailsApp.run(Application, args) 
    } 

    void doWithApplicationContext() { 
     ConstrainedProperty.removeConstraint(ConstrainedProperty.VALIDATOR_CONSTRAINT) 
     ConstrainedProperty.registerNewConstraint(ConstrainedProperty.VALIDATOR_CONSTRAINT, NamespaceValidatorConstraint.class) 
    } 
}