Grails get session variable from domain validator

2019-03-02 02:32发布

问题:

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