Delphi
Data Validation Options
Non-Database Data Validation:
- these techniques can be used for databound controls too
- use a control with set value options:
- checkbox, radiobutton, listbox, combobox,
lookupcombobox, calendar controls, etc.
- validating a TEdit component:
- set TEdit properties:
- MaxLength
- CharCase = [ecUpperCase, ecLowerCase]
- via TEdit's OnExit event to call a
validation function which raises an exception
which prevents user from exiting the control
- NB. if use the OnChange or OnKeyDown/Up
events, these will require much more coding as
are called each time user enters a character
Database Data Validation:
- Table-level validation:
- validation rules embedded within database
structure & exceptions are raised when a
record is attempted to be posted
- advantages:
- validation is stored with the data
- all applications that access the table
will be validated irrespective of
application's validations
- only needs to be created once, thus no
need to provide the validation code in
applications
- disadvantages:
- may delay user being notified of invalid
data until record or transaction is being
posted which may create confusion and
frustration
- limited flexibility depending of file
format
- some features can be over-ridden or
ignored
- examples:
- field data type (eg.
integer/date/float/char)
- primary keys to avoid duplicate unique
record ID's thereby producing key
violation exceptions
- validity checks (eg. Paradox tables) but
these are not utilised by Delphi ???
- mandatory fields (ie. cannot be left
blank) - not used by Delphi
- field format masks (eg. Paradox
"pictures") only used in Delphi
if use Infopower components
- triggers in SQL tables
- foreign keys (lookup tables) ensures data
must already exist in a lookup table
- Field-level validation:
- set input mask properties for
data entry fields (NOT the DisplayFormat
property!!):
- Delphi's EditMask:
- has 3 parts, each separated by
semicolons:
- mask (eg. ! = leading
blanks will not be saved,
0 = digit required)
- 0/1 to indicate whether
literal characters in
mask will be saved with
data (eg. hyphens)
- character used in mask to
refer to blank spaces
(eg. an underscore)
- Infopower's Paradox-style picture
masks:
- these are tested via InfoPower's OnInvalidValue
event
- can set user alerts via changing
controls color, etc via
InfoPower's OnCheckValue
event
- cannot define them in Delphi if
use Paradox table with picture
mask already defined, as it uses
them automatically if UsePictureMask
property set to true
- examples:
- # = any digit; ? = any
letter; & = any
letter, converted to
uppercase; @ = any
character;
- ! = any character
converted to uppercase; ;
= next character to be
used literally;
- [abc] = optional sequence
of characters abc
- { } = grouping operator
- eg. {Red, Green,
Blue}
- eg.
{Red,Gr[ay,een],B{l{ack,ue},rown},White,Yellow}
- * = repeat the following
character any number of
times:
- eg. *5{#} = specify 5
numbers in a row
- eg. *! = any number
or any character with all
letters converted to
uppercase
- eg. &*? = letters
with 1st letter
capitalised
- set TField "required"
property to true to prevent field being
left blank
- NB. if using EditMask with literals saved
then this may not trigger an exception if
edited then blanked!!
- via TField onValidation
event:
- eg. if field.value < 1 then raise
Exception.Create('Positive number
required');
- an exception occurring in this event
prevents the data from being posted and
displays the message
- see also non-database
data validation
- Record-level validation:
- via TDataset's "BeforePost"
event:
- can check for conditional validations
that depend on values in more than one
field
- raising an exception or calling abort
prevents the record from being posted
- Form-level validation:
- via Form's "OnCloseQuery"
event:
- NB. closing a form does not destroy it,
you need to call either Release
or Free methods to do that!
- this event is aborted by setting CanClose
:= False;
References:
- Delphi Informant Sept 1995 p50-56
-