我一直在嘗試在Grails項目中創建自定義約束(請參見下面的約束代碼)。自定義Grails約束似乎沒有工作
import org.codehaus.groovy.grails.validation.AbstractConstraint
import org.springframework.validation.Errors
class BuscaConstraint extends AbstractConstraint {
public static final String CONSTRAINT_NAME = "busca"
protected void processValidate(Object target, Object propertyValue, Errors errors) {
}
boolean supports(Class type) {
return type && String.class.isAssignableFrom(type);
}
String getName() {
return CONSTRAINT_NAME;
}
}
正如你所看到的,這個約束實際上並沒有驗證任何東西。相反,它只是標記來自定義腳手架代中的屬性渲染。 創建上面的類之後,我加在Config.groovy文件以下行:
ConstrainedProperty.registerNewConstraint(BuscaConstraint.CONSTRAINT_NAME, BuscaConstraint.class)
..和添加了這個約束類的屬性:
class ThatClass {
def someProperty
static constraints = { someProperty (unique:true, busca: "nome")
}
但是,如果我嘗試得到表達式的結果 ThatClass.constraints.someVariable.getAppliedConstraint("busca")
, 我得到的全部是null
。
我基於我的方法在一些博客文章中,如this one和constraint in Grails' github repo(但我不明白他們是如何配置的)。
我在做什麼錯? 最近Grails的自定義約束配置有變化嗎?