Validation Strategies


I am trying to decide what is the best strategy regarding form validation.

I have essayed successfully BeanValidation annotations and custom constraint definitions, however I find them a bit tedious, having to define the custom annotation and the validator classes, in order to ensure a small business logic and only one possible outcome message. I like the approach then it is the way to concentrate enitity related logic together facilitating maintainance, however when scoping slightly more complex coninstraints than mere field level validations, I find it too extensive to define a small set of rules to be validated at class/field levels whenever I have to validate logic involving more than one field at once.

I can remember I was able to include constraints that would rely on a simple method declared in the same entity class, saving the programmer to create 2 additional files for each validation.

The only way I would see this approach would be profitable from the coding perspective is if I could make verifications at the class level, being able to, in a customized fashion, add more than one violation/message pair from a single validator file, and being able to define the messages dynamically based on the logic verified.

I see the ConstraintValidatorContextImpl class, but I don’t know If I can manually add violation/message pairs manually.

Is that possible? Any other approach recommended?



Ok solved it. Found the piece of code at:

java - How to implement multiple JSR-303 validation messages for the same @Constraint? - Stack Overflow.



1 Like