Hi
When defining validation, you do not want to define a dedicated validation error message for each and every field, neither for each constraint. For instance the code generated by studio below enforces a percentage (number between 0 and 100).
@Digits(message = "{msg://com.busy.app.entity/percentageRequired}", integer = 3, fraction = 0)
@DecimalMax(message = "{msg://com.busy.app.entity/percentageRequired}", value = "100")
@DecimalMin(message = "{msg://com.busy.app.entity/percentageRequired}", value = "0")
@Column(name = "TRADE_DISCOUNT", precision = 3, scale = 0)
protected BigDecimal tradeDiscount;
First issue I had to define two constraints DecimalMin and DecimalMax (which is a recent Studio feature that is great to have BTW). It’s overdone to have two distinct messages for min and max, so I defined one for the two saying “percentage required”. Not a big deal.
Second issue, I would like to display the localized property name in the message, but I cannot.
By extension I would like to reuse my message for each percentage property of any entity. Displaying localized property name would come handy and save time dedicated to write up messages instead of code.
Bean validation could use specific tags similar to String.format() in messages like there are already in JPQL queries.
Tags would define placeholders to display :
- localized property name, e.g %lpn
- localized entity name, e.g %len
By looking through the doc it seems that everything is there with MessageTools. When creating the BeanValidator, class and property names are accessible so this seems possible to implement.
It would be nice to have it implemented directly for bean validation. Small feature, lots of time saved.
Mike