Search code examples
grailsgroovygrails-orm

Grails validation errors disappear from service to controller


When I do custom rejectValue in a service method grails loses that error(s) between service method and return to controller. This seems to happen when updating a row instance, but not when creating one.

In service

def specialValidation(petInstance){
       if(petInstance.petType.requiresStateId && !petInstance.StateId){
           petInstance.errors.rejectValue('StateId','StateId required');
       }
       println petInstance.errors //shows 1 error
       return petInstance;

}

In controller

...
   petInstance.properties=params;
   petInstance=petService.specialValidation(petInstance);
   println petInstance.errors //shows 0 errors

How is the error being lost when the instance changes hands from service to controller?


Solution

  • It can be because of transactional service. Service opens separate transaction for each method and clears entities after method end. You can find this mentioned in docs(read the last paragraph of part )

    I had the same problem. Than I've added NotTransactional annotation to validation method, and it helped. Errors were saved.