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
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.
Another option is to use org.springframework.validation.BeanPropertyBindingResult, which implements Errors. This object is of the same class of the BindingResult you recieve in Spring MVC controllers
Errors errors = new BeanPropertyBindingResult(objectToValidate, "objectName");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With