"Turn off" bean validation programmatically (javax.validation.constraints)
Asked Answered
V

1

9

For example, we have some entity in whitch several fields are validating with annotation @Pattern. This entity is used everywhere in project, but only in one place we need to "turn off" this validation.

Is there some way to do it programmatically or it is impossible?

Vesper answered 22/8, 2013 at 13:47 Comment(0)
A
14

You can use validation groups for this:

@Min(value = 18, message = "You have to be 18", groups = AdultsGroup.class)
public int age;

and then :

constraintViolations = validator.validate( person, AdultsGroup.class );

for checking min age, or:

constraintViolations = validator.validate( person, null );

for not checking this constraint.

You can read more details here: http://beanvalidation.org/1.1/spec/#constraintdeclarationvalidationprocess-groupsequence

Attraction answered 23/9, 2013 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.