Parameterized validation error messages in Studio

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

Hi,

We use default Expression Language integration with Hibernate Validator and do not replace placeholders manually. Messages can contain parameters and expressions. Parameters are enclosed in {} and represent either localized messages or annotation parameters, e.g. {min}, {max}, {value}. Expressions are enclosed in ${} and can include the validated value variable validatedValue, annotation parameters like value or min, and JSR-341 (EL 3.0) expressions.

Unfortunately, Hibernate Validator does not support substitution with current class and current property name. There is a known trick, described here: https://stackoverflow.com/questions/30809402/javax-validation-how-get-property-name-in-a-validation-message You could split your localized message up to two parts: before localized property name and after. Message should look like:


@NotNull(message = "{msg://com.company.entity/Currency.code.empty.part1}{msg://com.haulmont.refapp.core.entity/Currency.code}{msg://com.company.entity/Currency.code.empty.part2}")
protected String code;

We will try to improve this behavior in the future, see issue: https://youtrack.cuba-platform.com/issue/PL-9310

Thanks Yuriy,

Just to be sure I understand correctly, that would be in my case more {msg://com.company.entity/Currency.code} than {msg://com.haulmont.refapp.core.entity/Currency.code}, right ?

Yes, you are right, just change package.

:ticket: See the following issue in our bug tracker:

https://youtrack.cuba-platform.com/issue/PL-9310