java - How-to make Hibernate Validator stop validation on the first field violation? -


i have bean define multiple validation annotations each field e.g.

@notempty @pattern(regexp="(\\-?\\d)+") @min(value=1) string myfield; 

i have encountered two 1 problem cannot resolve in easy way.

  1. validation order of specified annotations each field random i.e. not happen in order annotations defined. believe @groupsequence not since defines group validation sequence, not annotation sequence. @tom correctly commented, violations reported set means there no 1:1 mapping between execution order of annotations , reported violations.
  2. i want invalidate 1 rule each field i.e. if not match pattern not try check if value >= 1. if set myfield "abc" report both @pattern , @min violations. setting failfast property of validator true not because i'm using same validator instance validate fields in bean, , stop validating other fields first violation whole bean encountered.

edit. tried implement custom composite constraint @reportassingleviolation. problem report same message violations involved in composition. not need.

suggestions, please?

it should possible create behavior want achieve using combination of validation groups , defined validation group sequence

quoting jsr-349 (aka. beanvalidation 1.1) spec, paragraph 4.4.2

processing group defined in section 4.6 ; if 1 of groups processed in sequence generates 1 or more constraint violations, groups following in sequence must not processed. ensures set of constraint evaluated if set of constraint valid.
highlighting me

this should relatively easy, going group sequence example

@groupsequence({yourclass.class, second.class, third.class}) public class yourclass {     @notnull     @pattern(regexp="(\\-?\\d)+", groups=second.class)     @min(value=1, groups=third.class)     string myfield; } 

with second , third defined simple marker-interfaces should trick. aware not validate fields until first constraint violation, until first overall violation.

this means of fields may become invalid after fixing other fields' violations.

if else fails can still reimplement behavioral components, validation provider. should able provide own implementation of constraintvalidatorcontext using validation.xml

be aware you'd have break contract of constraintvalidatorcontext.constraintviolationbuilder:

to create constraintviolation, 1 must call either 1 of addconstraintviolation() methods available in 1 of interfaces of fluent api.

if method called after addconstraintviolation() on constraintviolationbuilder or of associated objects illegalstateexception raised.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -