Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Validation - Programmatically bind to a Validator

I am trying to use Spring validation to validate my model populated by Jackson converter. So I have a java class,

class MyClass(){
    private String myString;
}

This class is populated by Jackson and I have the instance in my Java code. Have also defined a validator class like,

class MyValidator implements Validator {
    public boolean supports(Class<?> clazz) {
        return MyClass.class.equals(clazz);
    }   
    public void validate(Object object, Errors errors) {
           //Validation logic here
    }
}

Now what I wanted to do was to send the object to the validator and get the errors object, examine and proceed further. But, for calling

validate(Object object,Errors errors) 

I need an errors instance which I dont have. I cant use BindingResult or Errors. Please advise on how to proceed further.

like image 457
Raghav Avatar asked Aug 26 '11 22:08

Raghav


People also ask

Is @valid and @validated the same?

The @Valid annotation ensures the validation of the whole object. Importantly, it performs the validation of the whole object graph. However, this creates issues for scenarios needing only partial validation. On the other hand, we can use @Validated for group validation, including the above partial validation.

What is @valid annotation in Spring?

When Spring Boot finds an argument annotated with @Valid, it automatically bootstraps the default JSR 380 implementation — Hibernate Validator — and validates the argument. When the target argument fails to pass the validation, Spring Boot throws a MethodArgumentNotValidException exception.

What is @validated Spring?

The Spring MVC Validation is used to restrict the input provided by the user. To validate the user's input, the Spring 4 or higher version supports and use Bean Validation API. It can validate both server-side as well as client-side applications.

What does BindingResult do in Spring?

[ BindingResult ] is Spring's object that holds the result of the validation and binding and contains errors that may have occurred. The BindingResult must come right after the model object that is validated or else Spring will fail to validate the object and throw an exception.


1 Answers

You can do this manually in code with a DataBinder:

MyClass toValidate = new MyClass();
DataBinder binder = new DataBinder(toValidate);
binder.setValidator(new MyValidator());
binder.validate();
if (binder.getBindingResult().hasErrors()) {
    // oh noes!
}

Although if you've got a @ModelAttribute defined in a @Controller in spring-mvc, something like this should work (placed inside the relevant @Controller):

@ModelAttribute("myclass")
public MyClass myClass() {
    return new MyClass();
}

@InitBinder("myclass")
protected void initBinder(WebDataBinder binder) {
    binder.setValidator(new MyValidator());
}

@RequestMapping(value = "/do/something", method = POST)
public ModelAndView validatedRequest(@Valid @ModelAttribute("myclass") MyClass profile,
                                     BindingResult result) {
    if (result.hasErrors()) {
        // oh noes!
    }
}
like image 117
Tim Helmstedt Avatar answered Oct 19 '22 23:10

Tim Helmstedt