Posts Tagged ‘delphi;hcOPF’

hcOPF IsValid()

Monday, January 4th, 2010

Recently I have been enhancing the built-in validation capabilities of hcOPF in preparation for using it in a client project that requires visual error indicators, and flexible data entry.

I have introduced the concept of a Validator which is much like a database check constraint within the OPF layer, but it takes the concept much further.  The IsValid() method appears at both the object level and attribute definition level.  To enable support for any conceivable validation requirement, developers can override the default implementation on ThcObject, and attach custom validators to a ThcAttributeDef instance.

Validators are employed by the Mediators in the framework to take the string value from their associated control and after ensuring that it can be converted to the datatype of the associated attribute without a range error, their IsValid() method is called.  The prototype for IsValid at both the Object and Attribute level is as follows:

function IsValid(ValidationErrors :ThcValidationErrorList = nil):boolean;

IsValid() takes a list which is created by the developer and is used by the framework to collect any validation errors and their associated attributes.  The list can then be used to display the error information to the user, and controls containing invalid attribute values can be focused using the new UIObjectBinder.FocusControlForAttribute() method.

IsValid() has a default parameter of Nil so you aren’t obligated to pass it an instance of a ThcValidationErrorList.  This may seem strange, but it’s for a very good reason.  If you’re like me, and think that one of the best features of Delphi are TActions, then you don’t have to suffer the overhead of repeatedly clearing and populating the validation error list whenever a TAction.OnUpdate event handler executes.

For an example of how to use ThcValidators check out the ObjectListMgr project in the Demos/ObjectListMgr folder.