Grails get session variable from domain validator

為{幸葍}努か 提交于 2019-12-02 10:27:13

问题


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];
        })
    }
}

回答1:


See this post: http://www.mosbase.com/2011/07/grails-accessing-http-session-from.html



来源:https://stackoverflow.com/questions/6818016/grails-get-session-variable-from-domain-validator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!