Issue
I have a method like this:
public boolean validateMessage(String message, Errors errors) {
if (!StringUtils.hasLength(message)) {
errors.rejectValue(wrapperName + "message", "EMPTY_MESSAGE", EMPTY_MESSAGE_ERRORMSG) ;
return false ;
}
return true ;
}
I want to call this method with a new Errors
object, like:
boolean result = validateMessage("hi", new Errors()) ;
However, this kind of instantiation is not allowed for Errors
. Please advice.
If not with Errors
, can I achieve this using an empty (new) BindingResult
Solution
Errors
and BindingResult
are interfaces, therefore they cannot be instantiated. Your only option would be to use one of the classes which implements Errors
.
You could use org.springframework.validation.BindException
, this implements Errors
- see here for details.
Answered By - Jonathan
Answer Checked By - David Marino (JavaFixing Volunteer)