Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate custom validator, how to set propertyPath?
    text
    copied!<p>we've implemented a custom Validator.</p> <p>We are getting the message back OK, but not the propertyPath, how can we pull that back?</p> <p>implementation in our object</p> <pre><code>@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") }) </code></pre> <p>//the constraint class</p> <pre><code>@Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Constraint(validatedBy = RequiredIfSetValidator.class) public @interface RequiredIfSet { String field(); String dependentField(); Class&lt;?&gt;[] groups() default {}; Class&lt;? extends Payload&gt;[] payload() default {}; String message() default "Field Required"; String propertyPath() default ""; @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @interface List { RequiredIfSet[] value(); } } </code></pre> <p>//where we loop through invalid fields</p> <pre><code>List&lt;String[]&gt; invalidFields = new ArrayList&lt;String[]&gt;(); Iterator&lt;ConstraintViolation&lt;T&gt;&gt; iterator = cv.iterator(); while(iterator.hasNext()) { ConstraintViolation&lt;T&gt; 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; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload