Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Validation - How to retrieve errors.rejectValue in my Contoller?

I have my validate method in my TestValidator as follows

@Override
public void validate(Object target, Errors errors) {
    Test test = (Test) target;
    String testTitle = test.getTestTitle();

    //**ErrorCheck1** - This works, and I am able to pull the value in my controller
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "testTitle", "test.testTitle.projec", "My msg");


    if (testTitle != null && testTitle.length() < 4) {
        logger.error("Inside custom validation"+test.testTitle().length());

        //**Error Check2**
        //***HOW DO I RETRIEVE THE BELOW VALUE in My controller
        errors.rejectValue(testTitle, "test.testTitle.lessThen4");
        errors.addAllErrors(errors);
        logger.error("Entered If condition of validate");
    }
}

And my controller is

@RequestMapping(value = "/test", method = RequestMethod.PUT)
public ResponseEntity<BasicResponseDTO> newTest(@Valid  @RequestBody Test test, BindingResult result) {

    if (result.hasErrors()){
        logger.error("Entered Errors");
        BasicResponseDTO basicResponseDTO = new BasicResponseDTO();
        basicResponseDTO.setCode(ResponseCode.BAD_REQUEST);
        return new ResponseEntity<BasicResponseDTO>(basicResponseDTO, HttpStatus.BAD_REQUEST);
    }
}

When my ErrorCheck1 condition is activated, my IF condition inside the controller is able to retrieve it. However, in my ErrorCheck2, because of of the errors.rejectValue I immediately get an error on the console and am not able to gracefully handle the situation when the testTitle length is less than 4.

What is the alternative to errors.rejectValue so that I may handle the error in my controller ?

like image 669
HopeKing Avatar asked Dec 18 '22 06:12

HopeKing


2 Answers

Ok - Got it. All i had to do was change

errors.rejectValue(testTitle, "test.testTitle.lessThen4");

to

errors.reject(testTitle, "test.testTitle.lessThen4");

RejectValue is a Field error and is not global in nature. Reject is a Global error and can be accessed from inside the errors list in the controller.

From the Documentation

void reject(String errorCode, String defaultMessage);
Register a global error for the entire target object, using the given error description.

@Override
public void validate(Object target, Errors errors) {
Test test = (Test) target;
String testTitle = test.getTestTitle();

//**ErrorCheck1** - This works, and I am able to pull the value in my controller
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "testTitle", "test.testTitle.projec", "My msg");


if (testTitle != null && testTitle.length() < 4) {
    logger.error("Inside custom validation"+test.testTitle().length());

    //**Error Check2**
    //***HOW DO I RETRIEVE THE BELOW VALUE in My controller
    errors.reject(testTitle, "test.testTitle.lessThen4");
    errors.addAllErrors(errors);
    logger.error("Entered If condition of validate");
   }
}

Hope that helps someone.

like image 144
HopeKing Avatar answered Jan 25 '23 22:01

HopeKing


You cannot access the value directly but there is a way to include the value into error message

${validatedValue}

If you annotate a field like this

@Size(min = 10, message =”Phone number entered [${validatedValue}] is invalid. It must have at least {min} digits”)
private String phoneNumber;

and enter 123 your error message should be

Phone number entered [123] is invalid. It must have at least 10 digits. Thus you can access the value.

See https://raymondhlee.wordpress.com/2014/07/26/including-field-value-in-validation-message-using-spring-validation-framework-for-jsr-303/

like image 30
StanislavL Avatar answered Jan 26 '23 00:01

StanislavL