I have set up a MultiValidator
to check if the sum of 2 text fields equals the value of another field. Yet, for some reason, the validator is only validated once, upon initialization. Upon initialization, it always turns out successful as it should. The fail condition is only triggered at a later point within the wizard (condition to fail on 1st wizard page and validator on 3rd).
Following is the code for the databindings themselves as well as the MultiValidator
. The dbFactory
is a convenience class to set up the bindings themselves and that part works just fine.
fixedFeeAmountBinding = dbFactory.addBinding(fixedFeeAmountTxt, "fixedAmount", null,
new UpdateValueStrategy().setConverter(NumberToStringConverter
.fromBigDecimal(new com.ibm.icu.text.DecimalFormat(Formats.AMOUNT_FORMAT))));
variableFeeAmountBinding = dbFactory.addBinding(variableFeeAmountTxt, "variableAmount", null,
new UpdateValueStrategy().setConverter(NumberToStringConverter
.fromBigDecimal(new com.ibm.icu.text.DecimalFormat(Formats.AMOUNT_FORMAT))));
MultiValidator feeAmountValidaotr = new MultiValidator() {
@Override
protected IStatus validate() {
if (model.getVal().getClientChequePaymentRecordType().equals(ClientChequePaymentRecordType.INVOICE)
&& model.getVal().getInvoiceType() != null
&& !model.getVal().getInvoiceType().equals(InvoiceType.OTHER)) {
BigDecimal fixedAmountValue = new BigDecimal(
String.valueOf(((IObservableValue) fixedFeeAmountBinding.getTarget()).getValue())
.replaceAll(",", ""));
BigDecimal variableAmountValue = new BigDecimal(
String.valueOf(((IObservableValue) variableFeeAmountBinding.getTarget()).getValue())
.replaceAll(",", ""));
if (fixedAmountValue.add(variableAmountValue).compareTo(model.getVal().getInvoiceAmount()) == 0) {
return ValidationStatus.OK_STATUS;
}
return ValidationStatus.error("Fee Amounts don't add up to Invoice Amount");
}
return ValidationStatus.OK_STATUS; // <-- Doesn't update after returning this
}
};
dbFactory.addValidationStatusProvider(feeAmountValidaotr);
I also used to tip from my other question, but that didn't solve my issue this time.
What also makes this issue harder for me to understand is the fact, that the exact same code is working at another place in my code just fine.
Why is the MultiValidator
not updating when the binding updates? and How can I fix it?
If you need any more infos, don't hesitate to ask.
EDIT:
After some more testing, I found that ValidationStatusProvider
status does not seem to be updated with the new result of validate()
. Also I just found out that this code does not work properly in the other place, though the impact there is not that huge, as users can't change values to fail/succeed the first IF.
So basically the issue with the validator is that its not updating as soon as it returned the ValidationStatus.OK_STATUS
outside of all IFs.
It seems I found the solution to my issue... I replaced the model.getVal().getInvoiceAmount()
call with the binding value for the amount field and then moved the creation of the BigDecimals
in before the IF
. The whole code looks like this now:
MultiValidator feeAmountValidaotr = new MultiValidator() {
@Override
protected IStatus validate() {
// Only validate Fee Amounts if cheque can have fees
BigDecimal fixedAmountValue = new BigDecimal(
String.valueOf(((IObservableValue) fixedFeeAmountBinding.getTarget()).getValue())
.replaceAll(",", ""));
BigDecimal variableAmountValue = new BigDecimal(
String.valueOf(((IObservableValue) variableFeeAmountBinding.getTarget()).getValue())
.replaceAll(",", ""));
BigDecimal totalAmountValue = new BigDecimal(
String.valueOf(((IObservableValue) amountBinding.getTarget()).getValue()).replaceAll(",", ""));
if (getModelObject().getClientChequePaymentRecordType().equals(ClientChequePaymentRecordType.INVOICE)
&& getModelObject().getInvoiceType() != null
&& !getModelObject().getInvoiceType().equals(InvoiceType.OTHER)) {
if (fixedAmountValue.add(variableAmountValue).compareTo(totalAmountValue) == 0) {
return ValidationStatus.OK_STATUS;
}
return ValidationStatus.error("Fee Amounts don't add up to Invoice Amount");
}
return ValidationStatus.OK_STATUS;
}
};
dbf.addValidationStatusProvider(feeAmountValidaotr);