Hello,
I followed the documentation to create a percent field:
@NumberFormat(pattern = "##.##%")
@Column(name = "PERCENT_FIELD1")
protected Double percentField1;
But I noticed a strange behaviour: cuba will only consider the TextField input valid if the “%” sign is used:
It is strange that for regular number fields (pattern = “##.##”) the format is applied by default. But for percent number fields, it is not.
My first attempt was to add the “%” sign when the value of the field is changed. But the ValueChangeEvent
was only being fired after correctly filling the value, including the “%” sign. Luckily, there is the TextChangeEvent
that I could use.
@Subscribe("percentField2Field")
public void onPercentField2FieldTextChange(TextInputField.TextChangeEvent event) {
if (!event.getText().contains("%")) {
try {
NumberFormat numberFormat = DecimalFormat.getInstance(userSession.getLocale());
Number number = numberFormat.parse(event.getText());
event.getSource().setValue(number.doubleValue() / 100);
} catch (ParseException e) {
// Ignore error and do nothing
}
}
}
After implementing the above logic, it is working as expected (the number format is being applied).
Is this a bug? If not, is there an easier/cleaner way of acomplishing this result without having to customize every percent field?
P.S.: I’m attaching a test project in case anyone need to reproduce the error.
percenttest.zip (82.1 KB)
Cuba version: 7.2.4
Thanks.
Regards,
Peterson.