Validating Data

Before saving data on the server you can validate it and assign a handler function for any gotten response.

Shortly, validation contains 2 key points:

  1. Value checking;
  2. Processing in case of validation error.

Common

To implement the server-side validation of incoming data you can use the following events:

The beforeProcessing event fires for all types of operations, while other events fire only for the related operations, i.e. you can set different validation rules for different operations or for all of them at once.

The event will receive DataAction object as the parameter. This object can be used to retrieve the related data and allow/deny operation (please note, it contains only the data which was received from the client side, not the all data related to the record).

Value checking

To check the value of a field you should use the Data() method:

connector.BeforeInsert += new EventHandler<DataActionProcessingEventArgs>(
connector_BeforeInsert
);
 
void connector_BeforeProcessing(object sender, DataActionProcessingEventArgs e)
{
   if (string.IsNullOrEmpty(e.DataAction.Data[(TableField)"title"]))
   ...
}

Processing in case of validation error

In case of error you can choose one of two ways:

1) Use the predefined method SetInvalid() for error processing on the client side:

connector.BeforeInsert += new EventHandler<DataActionProcessingEventArgs>(
connector_BeforeInsert
);
 
void connector_BeforeProcessing(object sender, DataActionProcessingEventArgs e)
{
   if (string.IsNullOrEmpty(e.DataAction.Data[(TableField)"title"]))
   e.DataAction.SetInvalid("Book title cannot be empty!");
 
   if (string.IsNullOrEmpty(e.DataAction.Data[(TableField)"author"]))
   e.DataAction.SetInvalid("Book author cannot be empty!");
}

2) Assign your own processing on the client side through the dataProcessor's method defineAction() (see details here)

dp.defineAction("invalid",function(sid,response){
var message = response.getAttribute("message");
alert(message);
return true;// return false to cancel default data processing at all
})
Back to top