wicketwicketstuffwicket-7

How to get Label from IValidatable in Wicket 7


Is it possible to get the label of my component inside a validation? I need this label for a custom error message in my validation. It looks like:

"The value may not be less than {0}."

If my component has a label then i want to write it before like:

"LabelName: The value may no be less than {0}."

My component BigDecimalValidator doesn't know the BigDecimalTextfield.

tfiGV = new BigDecimalTextField("tfiGV", new Model<BigDecimal>());
tfiGV.setLabel(Model.of(Const_Labels.GV));
tfiGV.add(BigDecimalValidator.minimum(0));

The validatable of BigDecimalValidator can't reach the necessary label.

@Override
public void validate(IValidatable<BigDecimal> validatable) {
    // Doesn't work ((FormComponent<BigDecimal>) validatable).getLabel();

    if (((BigDecimal) validatable.getValue()).compareTo(BigDecimal.valueOf(minimum, 3)) == -1) {
            ValidationError valError = new ValidationError();
            valError.setMessage(getErrorMessageMin(minimum));
            validatable.error(valError);
    }
}

I know that the label can be reached by a constructor for BigDecimalValidator but this isn't a nice solution.


Solution

  • You can use {label} in your i18n message and Wicket will replace it with the form component's label.

    E.g. {label}: The value may not be less than {0}.