2012-05-18 59 views
1

我們已經實現了一個自定義驗證器。Hibernate自定義驗證器,如何設置propertyPath?

我們得到的消息是OK,但不是propertyPath,我們怎麼能把它拉回來?在我們的對象

實施

@RequiredIfSet.List({ 
     @RequiredIfSet(propertyPath = "reporterFirstName", field = "isFillingOutForSomeoneElse", dependentField = "reporterFirstName", message = "Reporter First may not be null"), 
     @RequiredIfSet(propertyPath = "reporterLastName", field = "isFillingOutForSomeoneElse", dependentField = "reporterLastName", message = "Reporter Last may not be null"), 
     @RequiredIfSet(propertyPath = "reporterContactPhone", field = "isFillingOutForSomeoneElse", dependentField = "reporterContactPhone", message = "Reporter Contact Phone may not be null"), 
     @RequiredIfSet(propertyPath = "reporterEmail", field = "isFillingOutForSomeoneElse", dependentField = "reporterEmail", message = "Reporter Email may not be null") 
}) 

//約束類

@Documented 
@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) 
@Constraint(validatedBy = RequiredIfSetValidator.class) 
public @interface RequiredIfSet { 
    String field(); 
    String dependentField(); 

    Class<?>[] groups() default {}; 
    Class<? extends Payload>[] payload() default {}; 
    String message() default "Field Required"; 
    String propertyPath() default ""; 

    @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) 
    @Retention(RetentionPolicy.RUNTIME) 
    @Documented 
    @interface List { 
     RequiredIfSet[] value(); 
    } 
} 

//這裏我們通過無效字段

List<String[]> invalidFields = new ArrayList<String[]>(); 
     Iterator<ConstraintViolation<T>> iterator = cv.iterator(); 
     while(iterator.hasNext()) { 
      ConstraintViolation<T> i = iterator.next(); 
      String property = i.getPropertyPath().toString(); 
      String message = i.getMessage(); 
      invalidFields.add(new String[] { property, message }); 
     } 
     EntityValidationDTO EVDTO = new EntityValidationDTO(); 
     EVDTO.setStatus("fail"); 
     EVDTO.setInvalidFields(invalidFields); 
     return EVDTO; 

回答

相關問題