Archive for April, 2011

hcOPF Validation Changes

Wednesday, April 20th, 2011

An oversight on my part when I wrote the validation framework within hcOPF was that users may enter values that exceed the range of the underlying datatype used by an hcOPF attribute. I assumed that the UI controls used would ensure that only values within the datatypes range would be assigned to the attribute.

It turns out this was a rather rash assumption considering the level of validation that some of the stock VCL controls provide. Consider the TEdit control for instance. In Delphi 7 it cannot even constrain the values input to only numbers. You have to choose a different control, namely the TMaskEdit, or a TSpinEdit to accomplish that. There are also many bugs in such controls that allow the user to input a value that should be considered invalid. The TSpinEdit, for instance, has a Max and Min Value which is utilized to validate the input when using the spinner buttons, but not when keying in the value directly.

In order to adhere to the DRY principle, and ensure that Range Check errors don’t occur for numeric attributes I had to modify the way the framework handles values from controls. Normally, hcOPF mediators would assign the value of the control to the attribute, and the attribute would be checked to see if it, and the resulting object as a whole was valid. This was to facilitate error indicators, and consistent validation message handling without throwing exceptions that products like Eurekalog would trap and report as errors.

So validation has now been re-vamped such that the Attribute performs a range check before attempting to assign the value in it’s native setter (the one matching it’s datatype). Instead of throwing an exception like a TField does, the attribute ignores the new value and sets an error message so the error indicator framework can reflect the condition in the UI. If you don’t use the error indicator framework, you would have to test if the lastValidationMessage is not empty and display it in a message dialog, and then refocus the control.