I have a dropdown box and a TextField. The value of the text field must begin with a unique number depending of the selection. If not an error message will be shown.
Depending on the selection the error message looks like this:
the number in the "TextField" must begin with 101 for selection1
or
the number in the "TextField" must begin with 102 for selection2
etc.
I have written a custom validator to validate the TextField.
What I don't like is that for each error I have a resource key with its corresponding value for the error messages:
@Override
protected void onValidate(IValidatable<String> validatable) {
if(selection.equals(selections1) && !beginsWithGoodNumber){
error(validatable, "error.selection1");
}else if(selection.equals(selections1) && !beginsWithGoodNumber){
error(validatable, "error.selection2");
}else if(selection.equals(selections1) && !beginsWithGoodNumber){
error(validatable, "error.selection3");
}
}
in the property file I have:
error.selection1 = the number in the '${label}' must begin with 101 for selection1
error.selection2 = the number in the '${label}' must begin with 102 for selection2
error.selection3 = the number in the '${label}' must begin with 103 for selection3
error.selection4 = the number in the '${label}' must begin with 104 for selection4
I would like to have something like this in the property file:
error.selection = the number in the '${label}' must begin with {number} for {selection}
where {number}
and {selection}
are variables.
Is this possible?
Thanks.
Assuming your validator extends AbstractValidator, you can pass a Map of variables to #error().
Or see AbstractRangeValidator on how to create a ValidationError, set variables on it and report it to the component.