Grails get session variable from domain validator

2019-03-02 02:42发布

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

登录 后发表回答