Note that there are some explanatory texts on larger screens.

plurals
  1. POFormatting (string-) array as part of bean-validation message
    primarykey
    data
    text
    <p>I created my first custom validation annotation with validator class as inner class (which i find quite well-arranged). It looks like this:</p> <pre><code>@Target( { ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = {OneOfGroupNotNull.Validator.class}) @Documented public @interface OneOfGroupNotNull { // custom annotation properties String[] fields(); // required by JSR-303 String message() default "One of group must be not null. {fields}"; Class&lt;?&gt;[] groups() default {}; Class&lt;? extends Payload&gt;[] payload() default {}; public class Validator implements ConstraintValidator&lt;OneOfGroupNotNull, Object&gt; { private String[] fields; @Override public boolean isValid(Object bean, ConstraintValidatorContext cvc) { int countNotNull = 0; for (String field : fields) { try { String property = BeanUtils.getProperty(bean, field); if (property != null) { countNotNull++; } } catch (Exception ex) { throw new RuntimeException("Validation for field " + field + " of type " + bean.getClass()+ " raised exception.", ex); } } return countNotNull == 1; } @Override public void initialize(OneOfGroupNotNull a) { fields = a.fields(); } } } </code></pre> <p>A bean class that is annotated with this validator may look like this:</p> <pre><code>@OneOfGroupNotNull(fields = {"a", "b", "c"}) public interface MyBean { String getA(); Rarity getB(); Boolean getC(); } </code></pre> <p>The problem is that I cannot find a way to format the string array "fields". It just takes the to string method which results in something like: One of group must be not null. [Ljava.lang.String;@157d954</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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