Create a validator object from a class
Because you don't want to repeat your code or use the same validator every times in multiple form. I have integrated an annotation @Rules("rules you want')
which allows creating a rule on a field in a class.
How to do that ?
First we need to create a simple class:
public class UserDto {
private String name;
private String email;
private int age;
}
Then add @Rule on each fields:
public class UserDto {
@Rules("optional | min_length: 3 | max_length: 20")
private String name;
@Rules("email | max_length: 50")
private String email;
@Rules("range: 18, 100")
private int age;
}
And next when you want to get the validator from this class just do:
Validator validator = Validator.validatorFromClass(UserDto.class);
Don't forget to inject form implementation else you will have some errors.
You can add other validation rules.