bindingResult = new BeanPropertyBindingResult(object, object.getClass().getName());
bindingResult.addError(new ObjectError(bindingResult.getObjectName(), e.getMessage()));
}
if (domain != null && bindingResult != null) {
BindingResult newResult = new ValidationErrors(object);
for (Object error : bindingResult.getAllErrors()) {
if (error instanceof FieldError) {
FieldError fieldError = (FieldError)error;
final boolean isBlank = BLANK.equals(fieldError.getRejectedValue());
if (!isBlank) {
newResult.addError(fieldError);
}
else if (domain.hasPersistentProperty(fieldError.getField())) {
final boolean isOptional = domain.getPropertyByName(fieldError.getField()).isOptional();
if (!isOptional) {
newResult.addError(fieldError);
}
}
else {
newResult.addError(fieldError);
}
}
else {
newResult.addError((ObjectError)error);
}
}
bindingResult = newResult;
}
MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(object.getClass());
if (mc.hasProperty(object, "errors")!=null && bindingResult!=null) {
ValidationErrors errors = new ValidationErrors(object);
errors.addAllErrors(bindingResult);
mc.setProperty(object,"errors", errors);
}
return bindingResult;
}