Documents > BPS V2 Script API
bps::validator::Base Class Reference

The Base class serves as base for all field validators. More...

List of all members.

Public Member Functions

Boolean checkDelete (Object aRecord)
 Checks if the record data is valid for delete.
Boolean checkInsert (Object aRecord)
 Checks if the record data is valid for insert.
Boolean checkUpdate (Object aRecord)
 Checks if the record data is valid for update.

Protected Member Functions

 Base (Datastore aDatastore, String aTable)
Boolean checkLength (Object aRecord, String aColumn)
 Checks if the value length exceeds the maximum column length.
Boolean checkMask (Object aRecord, String aColumn)
 Checks if the column value is valid for the mask.
Boolean checkNotNull (Object aRecord, String aColumn)
 Checks if the given column is not null.
Boolean checkReference (Object aRecord, String aColumn, String aRefTable)
 Checks if the given column is either null, or references the primary key of an existing row in aRefTable.
Boolean checkUnique (Object aRecord, String aColumn)
 Checks if the column value is unique within the whole table.
Boolean checkUnique2 (Object aRecord, String aColumn1, String aColumn2)
 Checks if the combination of 2 columns is unique.
Boolean checkUniqueNotDeleted (Object aRecord, String aColumn)
 Checks if the column value is unique within the whole table, excluding records with "deleted" status from the check ('d' in column c_status).
Boolean checkUniqueOrNull (Object aRecord, String aColumn)
 Checks if the column value is either NULL or unique within the whole table.
void clear ()
 Clears the status by setting lastStatus = bps.FieldValidator.StatusOK and lastColumn = ''.

Properties

Datastore datastore
 [protected] Datastore object used by the validator.
String lastColumn
 Column on which an error occurred, set by the check() execution.
Status lastStatus
 The status set by the check() execution.
Object masks
 [protected] Mask cache used by checkMask().
String table
 [protected] The main table handled by the validator.

Detailed Description

The Base class serves as base for all field validators.

The validator needs to be implemented in a distinct js file and placed somewhere in the file system, typically in the BPS program folder under scripts/validators. The file must be registered in the global system settings in folder Validators as key / value pair, where the key is the table name and the value is the absolute or relative script file path/name.

Below is a complete sample validator implementation for the t_countries table. (Check out scripts/validators/default/* for more samples)

 // Load validator base class:
 importExtension('bps.validator');  
 
 // Implement validator, name must be 'BpsFieldValidator' in any case:
 function BpsFieldValidator(aDatastore)
 {
     // call constructor of the base class:
     bps.validator.Base.call(this, aDatastore, 't_countries');
 } // BpsFieldValidator
 
 // Set prototype of our validator to be the base class:
 BpsFieldValidator.prototype = new bps.validator.Base();
 
 // Implement our custom check method, using the properties and
 // messages of the base class:
 BpsFieldValidator.prototype.checkUpdate = function(aRecord)
 {
     // Clear the last results
     this.lastStatus = bps.FieldValidator.StatusOK;
     this.lastColumn = '';

     // Do the checks, returning true on success:
     return this.checkMask(aRecord, 'c_code')
         && this.checkUnique(aRecord, 'c_code')
         && this.checkMask(aRecord, 'c_name');
 } // checkUpdate

Constructor & Destructor Documentation

bps::validator::Base::Base ( Datastore  aDatastore,
String  aTable 
) [protected]
Parameters:
aDatastoreDatastore object in connected state.
aTableName of the main table the validator operates on.

Member Function Documentation

Boolean bps::validator::Base::checkDelete ( Object  aRecord)

Checks if the record data is valid for delete.

Stops on first fail and sets the values of properties lastStatus and lastColumn.

Parameters:
aRecordThe record to check, where the properties hold the column values.
Returns:
True if all column checks passed, false otherwise.
Boolean bps::validator::Base::checkInsert ( Object  aRecord)

Checks if the record data is valid for insert.

Stops on first fail and sets the values of properties lastStatus and lastColumn.

Parameters:
aRecordThe record to check, where the properties hold the column values.
Returns:
True if all column checks passed, false otherwise.
Boolean bps::validator::Base::checkLength ( Object  aRecord,
String  aColumn 
) [protected]

Checks if the value length exceeds the maximum column length.

Sets lastStatus and lastColumn in case the check fails, and returns false.

The check is only done when aRecord contains aColumn as property, otherwise it will just return true.

Parameters:
aRecordThe record to check, where the properties hold the column values.
aColumnName of the column value to check.
Returns:
True if ok, false otherwise.
Boolean bps::validator::Base::checkMask ( Object  aRecord,
String  aColumn 
) [protected]

Checks if the column value is valid for the mask.

Sets lastStatus and lastColumn in case the check fails, and returns false.

The check is only done when the mask for aColumn is found and aRecord contains aColumn as property, otherwise it will just return true.

Parameters:
aRecordThe record to check, where the properties hold the column values.
aColumnName of the column value to check.
Returns:
True if ok, false otherwise.
Boolean bps::validator::Base::checkNotNull ( Object  aRecord,
String  aColumn 
) [protected]

Checks if the given column is not null.

Parameters:
aRecordThe record to check, where the properties hold the column values.
aColumnName of the column value to check.
Returns:
True if ok, false otherwise.
Boolean bps::validator::Base::checkReference ( Object  aRecord,
String  aColumn,
String  aRefTable 
) [protected]

Checks if the given column is either null, or references the primary key of an existing row in aRefTable.

Parameters:
aRecordThe record to check, where the properties hold the column values.
aColumnName of the column value to check.
aRefTableName of the referenced table.
Returns:
True if ok, false otherwise.
Boolean bps::validator::Base::checkUnique ( Object  aRecord,
String  aColumn 
) [protected]

Checks if the column value is unique within the whole table.

Sets lastStatus and lastColumn in case the check fails, and returns false. The check is only done when aRecord contains c_key and aColumn as properties, otherwise it will return true.

Parameters:
aRecordThe record to check, where the properties hold the column values.
aColumnName of the column value to check.
Returns:
True if ok, false otherwise.
Boolean bps::validator::Base::checkUnique2 ( Object  aRecord,
String  aColumn1,
String  aColumn2 
) [protected]

Checks if the combination of 2 columns is unique.

Sets lastStatus and lastColumn in case the check fails, and returns false.

The check is only done when aRecord contains c_key, aColumn1 and aColumn2 as properties, otherwise it will return true.

Parameters:
aRecordThe record to check, where the properties hold the column values.
aColumn1Name of the first column.
aColumn2Name of the second column.
Returns:
True if ok, false otherwise.
Boolean bps::validator::Base::checkUniqueNotDeleted ( Object  aRecord,
String  aColumn 
) [protected]

Checks if the column value is unique within the whole table, excluding records with "deleted" status from the check ('d' in column c_status).

Used to check the column c_id in tables t_articles and t_partners for example. Sets lastStatus and lastColumn in case the check fails, and returns false.

Parameters:
aRecordThe record to check, where the properties hold the column values.
aColumnName of the column value to check.
Returns:
True if ok, false otherwise.
Boolean bps::validator::Base::checkUniqueOrNull ( Object  aRecord,
String  aColumn 
) [protected]

Checks if the column value is either NULL or unique within the whole table.

Sets lastStatus and lastColumn in case the check fails, and returns false.

The check is only done when aRecord contains c_key and aColumn as properties, otherwise it will return true.

Parameters:
aRecordThe record to check, where the properties hold the column values.
aColumnName of the column value to check.
Returns:
True if ok, false otherwise.
Boolean bps::validator::Base::checkUpdate ( Object  aRecord)

Checks if the record data is valid for update.

Stops on first fail and sets the values of properties lastStatus and lastColumn.

Parameters:
aRecordThe record to check, where the properties hold the column values.
Returns:
True if all column checks passed, false otherwise.
void bps::validator::Base::clear ( ) [protected]

Clears the status by setting lastStatus = bps.FieldValidator.StatusOK and lastColumn = ''.

Usually called at begin of check().


Property Documentation

Datastore bps::validator::Base::datastore [read]

[protected] Datastore object used by the validator.

To be set via constructor only, dont change after.

String bps::validator::Base::lastColumn [read, write]

Column on which an error occurred, set by the check() execution.

Empty in case all checks passed.

Status bps::validator::Base::lastStatus [read, write]

The status set by the check() execution.

Set to bps.FieldValidator.StatusOK when all checks passed.

Object bps::validator::Base::masks [read, write]

[protected] Mask cache used by checkMask().

Properties are the already fetched masks for columns previously processed. For columns where no mask was found, an empty string is stored as mask.

String bps::validator::Base::table [read]

[protected] The main table handled by the validator.

To be set via constructor only, dont change after.