3

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?

1
  • Can you resolve the problem?
    – iberck
    Nov 4, 2015 at 5:06

3 Answers 3

3

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.

1

Well I did something simular :

 orderService.validate(order, params)
 if (order.hasErrors()) {
      return render(view: 'create', model: [order: order])
}

In the Service I do some validation like this:

if (end.before(start)) { 
        order.errors.rejectValue("end", '', 'ERROR');
}

The different to yours is that i didn't set the errorCode but the message at itself, have a look at the rejectValue Methods:

void rejectValue(String field, String errorCode);
void rejectValue(String field, String errorCode, String defaultMessage);

You could also try to use the rejectValue method like me, maybe it helps.

0

I found you can also avoid this by using

 MyDomain.read(id)

instead of

MyDomain.get(id)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.