Validation recommendations based on Spring MVC actions

I am trying to perform validation using Spring validation. I am wondering what is the best way to perform validation, which depends mainly on the user's actions, in the future I have three different approaches, but I don’t know which one is better.

Suppose we have the following Foo class for validation and many different validation rules depending on the action performed by the user.

public class Foo { private String name; private int age; private String description; private int evaluation; // getters, setters } 

What is the best way to perform these checks (for example: at the time of creation, only the name and age are required during the validity of the evaluation, I only need the evaluation that needs to be checked, etc.)

solution 1: one validator class for validation rule

 public class CreateFooValidator implements Validator { //validation for action create } public class EvaluateFooValidator implements Validator { //validation for evaluate action } 

solution 2: one validator class with several methods

 public class FooValidator implements Validator { @Override public boolean supports(Class<?> clazz) { return Foo.class.equals(clazz); } //the default validate method will be used to validate during create action @Override public void validate(Object target, Errors errors) { //validation required } //method to validate evaluate action public void validateFooEvaluation(Object target, Errors errors) { //validation required } //other validation methods for other actions } 

solution 3: Additional property action in class Foo, one validator

 public class Foo { private String name; private int age; private String description; private int evaluation; private String actionOnFoo; // getters, setters } public class FooValidator implements Validator { private final Foo foo = (Foo) target; @Override public boolean supports(Class<?> clazz) { return Foo.class.equals(clazz); } @Override public void validate(Object target, Errors errors) { //test which action is performed by user if ("create".equals(foo.getActionOnFoo())) { //call for private method that performs validation for create action } } //all private methods } 

What is the best solution among 3 or another solution, if any? Thanks!

+6
source share
2 answers

Use JSR-303 validation groups that are also supported by @Validated with Spring MVC 3.1.

So, for every action you have to have a method in your controller. Create a validation group for each possible action that has a different set of rules, for example

 public interface Create { } public interface Evaluate { } 

Annotate Foo with validation annotations, including a group like

 public class Foo { @NotNull(groups = { Create.class, Evaluate.class }) private String name; ... @Min(value = 1, message = "Evaluation value needs to be at least 1", groups = { Evaluate.class }) private int evaluation; ... } 

Then annotate the Foo argument of your controller methods with the appropriate @Validated annotation, for example. @Validated({Evaluate.class}) for the evaluate controller evaluate .

Here you can find another example (see point 2): http://blog.goyello.com/2011/12/16/enhancements-spring-mvc31/

Update: Alternatively, if for some reason you cannot / do not want to use @Validated , you can use the Validator instance with injection and pass the groups to the validate method. How it was done before Spring 3.1 (as it is in the article in your comment).

+9
source

Depends on what you want to do. All solutions are good if you apply the same in all your validators and you want to check some business logic, and not just if it is zero or not (for this purpose you can use the @Valid annotation and @Not null annotation in your objects )

For me, if I have a Foo object, I only need one validator for this object, and then I have several methods for checking the data, which depend on my needs for controllers, for example, one to save a new Foo or another to check before update etc.

+1
source

Source: https://habr.com/ru/post/954922/


All Articles