Thursday, March 09, 2006

BaseCompareValidator.Compare bug

In my use of the Noogen.Validator component for doing WinForms validation in a VS2005 project, I could not get validation of required 'currency' (decimal) fields to work properly. The Noogen datatype based validation rules utilizes the BaseCompareValidator of the System.Web.UI.WebControls namespace to do the .CanConvert() and .Compare() tests. This allows for Noogen to do culture aware validation of dates and numbers by reusing .NET framework components.

BaseCompareValidator.CanConvert(String, ValidationDataType)

BaseCompareValidator.Compare(String, String, ValidationCompareOperator, ValidationDataType)

Validation of required fields uses ValidationCompareOperator set to Equal to compare the entered value against the specified initial value of the field (typically ""), and the the required check returns true when these values differs. But this mechanism did not work as expected.

After a lot of frustration and a bit of debugging, I got these strange results using the 'Immediate' windows in VS2005:

? ValidationUtil.Compare("123", "", vco, vdt)
? ValidationUtil.Compare("123", "0", vco, vdt)

Thus, the BaseCompareValidator considers the two 'currency' fields "123" and "" to be equal, which is clearly not true. The 'culture-neutral format' compare has the same bug.

The Noogen.Validator discussion thread suggests that this error also applies to 'double' also. In fact, it might apply to all datatypes other than 'string'.

No comments: