Grails从域验证器获取会话变量 [英] Grails get session variable from domain validator
问题描述
我确定这是一种常见的情况,但我还没有找到任何答案。我有一个包含当前登录用户的会话范围变量,我需要通过域对象中的自定义验证器执行条件验证。有没有一种方法可以在验证程序中将当前用户从会话范围中取出,或者有另一种方法可以执行此操作,请记住,我希望能够从我的验证程序中返回特定字段的错误(例如 if(isBlank(it))return ['blank','summary',Presentation];
)
basic结构:
class MyDomain
{
String aProperty;
static constraints =
{
aProperty(validator:{
if(isAdmin())return true;
if(isBlank(it))return ['blank' ,'summary',Presentation];
})
}
}
看到这篇文章: http://www.mosbase.com/2011/07/grails-accessing-http-session-from.html
Im sure this is a common scenario, but I haven't found any answers. I have a session-scoped variable that holds the currently signed in user and I need to perform conditional validation by way of a custom validator in a domain object. Is there a way to get the the current user from the session scope while in a validator, or is there perhaps another way to do this, keeping in mind that I want to be able to return the errors for specific fields from my validator(e.g. if(isBlank(it))return ['blank','summary',Presentation];
)
basic struture:
class MyDomain
{
String aProperty;
static constraints =
{
aProperty(validator:{
if(isAdmin())return true;
if(isBlank(it))return ['blank','summary',Presentation];
})
}
}
See this post: http://www.mosbase.com/2011/07/grails-accessing-http-session-from.html
这篇关于Grails从域验证器获取会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!