1

I need to validate a POJO, without using the annotation @Valid as paramether of a method.

I'm stuck at this point:

    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<Menu>> constraintViolations=validator.validate(menu);
    //Menu is my Pojo

    if (!constraintViolations.isEmpty()) {
        Iterator itr = constraintViolations.iterator();
        while(itr.hasNext()){
            Object o = itr.next();
        }               
    }

It seems to work, since inside "itr" I have stuff like this::

oConstraintViolationImpl{interpolatedMessage='size must be between 3 and 50', propertyPath=titolo, rootBeanClass=class com.springgestioneerrori.model.Menu, messageTemplate='{javax.validation.constraints.Size.message}'}

Now, my question is: how can I add "itr" values to BindingResult??? Probably I should cast something...somehow...

Thank you

1 Answer 1

1
@Test
public void testBindingResult(BindingResult result,Set<ConstraintViolation<String>> violations){
    for (  ConstraintViolation<String> constraintViolation : violations) {
        ObjectError error=new ObjectError("object",constraintViolation.getMessage());
        result.addError(error);
    }
}

you should create object of type ObjectError here is a spring doc for the class and you should add it to binding result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.